Skip to content

Commit 5b536d5

Browse files
committed
avoid assertion check when possible
1 parent bb14838 commit 5b536d5

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34515,31 +34515,35 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3451534515
}
3451634516

3451734517
function checkAssertionWorker(node: JSDocTypeAssertion | AssertionExpression, checkMode: CheckMode | undefined) {
34518-
let type: TypeNode;
34518+
let typeNode: TypeNode;
3451934519
let expression: Expression;
3452034520
switch (node.kind) {
3452134521
case SyntaxKind.AsExpression:
3452234522
case SyntaxKind.TypeAssertionExpression:
34523-
type = node.type;
34523+
typeNode = node.type;
3452434524
expression = node.expression;
3452534525
break;
3452634526
case SyntaxKind.ParenthesizedExpression:
34527-
type = getJSDocTypeAssertionType(node);
34527+
typeNode = getJSDocTypeAssertionType(node);
3452834528
expression = node.expression;
3452934529
break;
3453034530
}
3453134531
const exprType = checkExpression(expression, checkMode);
34532-
if (isConstTypeReference(type)) {
34532+
if (isConstTypeReference(typeNode)) {
3453334533
if (!isValidConstAssertionArgument(expression)) {
3453434534
error(expression, Diagnostics.A_const_assertions_can_only_be_applied_to_references_to_enum_members_or_string_number_boolean_array_or_object_literals);
3453534535
}
3453634536
return getRegularTypeOfLiteralType(exprType);
3453734537
}
34538-
const links = getNodeLinks(node);
34539-
links.assertionExpressionType = exprType;
34540-
checkSourceElement(type);
34541-
checkNodeDeferred(node);
34542-
return getTypeFromTypeNode(type);
34538+
checkSourceElement(typeNode);
34539+
const type = getTypeFromTypeNode(typeNode);
34540+
// See if we need to check whether the expression and asserted types are comparable.
34541+
if (!isErrorType(type) && !((exprType.flags | type.flags) & (TypeFlags.AnyOrUnknown | TypeFlags.Never))) {
34542+
const links = getNodeLinks(node);
34543+
links.assertionExpressionType = exprType;
34544+
checkNodeDeferred(node);
34545+
}
34546+
return type;
3454334547
}
3454434548

3454534549
function checkAssertionDeferred(node: JSDocTypeAssertion | AssertionExpression) {
@@ -34551,15 +34555,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3455134555
Debug.assertIsDefined(links.assertionExpressionType);
3455234556
const exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(links.assertionExpressionType));
3455334557
const targetType = getTypeFromTypeNode(type);
34554-
if (!isErrorType(targetType)) {
34555-
addLazyDiagnostic(() => {
34556-
const widenedType = getWidenedType(exprType);
34557-
if (!isTypeComparableTo(targetType, widenedType)) {
34558-
checkTypeComparableTo(exprType, targetType, errNode,
34559-
Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first);
34560-
}
34561-
});
34562-
}
34558+
addLazyDiagnostic(() => {
34559+
const widenedType = getWidenedType(exprType);
34560+
if (!isTypeComparableTo(targetType, widenedType)) {
34561+
checkTypeComparableTo(exprType, targetType, errNode,
34562+
Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first);
34563+
}
34564+
});
3456334565
}
3456434566

3456534567
function checkNonNullChain(node: NonNullChain) {

0 commit comments

Comments
 (0)