Skip to content

Revert "Defer type comparability check for assertions" #53879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 10 additions & 38 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ import {
JSDocSatisfiesTag,
JSDocSignature,
JSDocTemplateTag,
JSDocTypeAssertion,
JSDocTypedefTag,
JSDocTypeExpression,
JSDocTypeLiteral,
Expand Down Expand Up @@ -1045,6 +1044,7 @@ import {
TypeReferenceSerializationKind,
TypeReferenceType,
TypeVariable,
UnaryExpression,
unescapeLeadingUnderscores,
UnionOrIntersectionType,
UnionOrIntersectionTypeNode,
Expand Down Expand Up @@ -34434,14 +34434,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getReturnTypeOfSignature(signature);
}

function checkAssertion(node: AssertionExpression, checkMode: CheckMode | undefined) {
function checkAssertion(node: AssertionExpression) {
if (node.kind === SyntaxKind.TypeAssertionExpression) {
const file = getSourceFileOfNode(node);
if (file && fileExtensionIsOneOf(file.fileName, [Extension.Cts, Extension.Mts])) {
grammarErrorOnNode(node, Diagnostics.This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Use_an_as_expression_instead);
}
}
return checkAssertionWorker(node, checkMode);
return checkAssertionWorker(node, node.type, node.expression);
}

function isValidConstAssertionArgument(node: Node): boolean {
Expand Down Expand Up @@ -34472,42 +34472,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return false;
}

function checkAssertionWorker(node: JSDocTypeAssertion | AssertionExpression, checkMode: CheckMode | undefined) {
const { type, expression } = getAssertionTypeAndExpression(node);
const exprType = checkExpression(expression, checkMode);
function checkAssertionWorker(errNode: Node, type: TypeNode, expression: UnaryExpression | Expression, checkMode?: CheckMode) {
let exprType = checkExpression(expression, checkMode);
if (isConstTypeReference(type)) {
if (!isValidConstAssertionArgument(expression)) {
error(expression, Diagnostics.A_const_assertions_can_only_be_applied_to_references_to_enum_members_or_string_number_boolean_array_or_object_literals);
}
return getRegularTypeOfLiteralType(exprType);
}
checkSourceElement(type);
checkNodeDeferred(node);
return getTypeFromTypeNode(type);
}

function getAssertionTypeAndExpression(node: JSDocTypeAssertion | AssertionExpression) {
let type: TypeNode;
let expression: Expression;
switch (node.kind) {
case SyntaxKind.AsExpression:
case SyntaxKind.TypeAssertionExpression:
type = node.type;
expression = node.expression;
break;
case SyntaxKind.ParenthesizedExpression:
type = getJSDocTypeAssertionType(node);
expression = node.expression;
break;
}

return { type, expression };
}

function checkAssertionDeferred(node: JSDocTypeAssertion | AssertionExpression) {
const { type, expression } = getAssertionTypeAndExpression(node);
const errNode = isParenthesizedExpression(node) ? type : node;
const exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(expression)));
exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(exprType));
const targetType = getTypeFromTypeNode(type);
if (!isErrorType(targetType)) {
addLazyDiagnostic(() => {
Expand All @@ -34518,6 +34492,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
});
}
return targetType;
}

function checkNonNullChain(node: NonNullChain) {
Expand Down Expand Up @@ -37756,7 +37731,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return checkSatisfiesExpressionWorker(node.expression, getJSDocSatisfiesExpressionType(node), checkMode);
}
if (isJSDocTypeAssertion(node)) {
return checkAssertionWorker(node, checkMode);
const type = getJSDocTypeAssertionType(node);
return checkAssertionWorker(type, type, node.expression, checkMode);
}
}
return checkExpression(node.expression, checkMode);
Expand Down Expand Up @@ -37837,7 +37813,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return checkTypeOfExpression(node as TypeOfExpression);
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
return checkAssertion(node as AssertionExpression, checkMode);
return checkAssertion(node as AssertionExpression);
case SyntaxKind.NonNullExpression:
return checkNonNullAssertion(node as NonNullExpression);
case SyntaxKind.ExpressionWithTypeArguments:
Expand Down Expand Up @@ -44951,10 +44927,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.JsxElement:
checkJsxElementDeferred(node as JsxElement);
break;
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.ParenthesizedExpression:
checkAssertionDeferred(node as AssertionExpression | JSDocTypeAssertion);
}
currentNode = saveCurrentNode;
tracing?.pop();
Expand Down
32 changes: 0 additions & 32 deletions tests/baselines/reference/classVarianceCircularity.js

This file was deleted.

42 changes: 0 additions & 42 deletions tests/baselines/reference/classVarianceCircularity.symbols

This file was deleted.

45 changes: 0 additions & 45 deletions tests/baselines/reference/classVarianceCircularity.types

This file was deleted.

16 changes: 0 additions & 16 deletions tests/cases/compiler/classVarianceCircularity.ts

This file was deleted.