From 46cd4219ddbf243c34088bc0682f7f1da5a4b261 Mon Sep 17 00:00:00 2001 From: bgenia Date: Sun, 17 Nov 2024 15:04:30 +0300 Subject: [PATCH] Remove getNonNullableType application in checkIdentifier --- src/compiler/checker.ts | 3 +-- .../wrongEvolvingTypeUnderNonNullAssertion.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/wrongEvolvingTypeUnderNonNullAssertion.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cc8a7f7428f..0b91bfd9c44 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -30525,8 +30525,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const initialType = isAutomaticTypeInNonNull ? undefinedType : assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) : typeIsAutomatic ? undefinedType : getOptionalType(type); - const flowType = isAutomaticTypeInNonNull ? getNonNullableType(getFlowTypeOfReference(node, type, initialType, flowContainer)) : - getFlowTypeOfReference(node, type, initialType, flowContainer); + const flowType = getFlowTypeOfReference(node, type, initialType, flowContainer); // A variable is considered uninitialized when it is possible to analyze the entire control flow graph // from declaration to use, and when the variable's declared type doesn't include undefined but the // control flow based type does include undefined. diff --git a/tests/cases/fourslash/wrongEvolvingTypeUnderNonNullAssertion.ts b/tests/cases/fourslash/wrongEvolvingTypeUnderNonNullAssertion.ts new file mode 100644 index 00000000000..86846551f76 --- /dev/null +++ b/tests/cases/fourslash/wrongEvolvingTypeUnderNonNullAssertion.ts @@ -0,0 +1,14 @@ +/// + +// Issue #60514 + +// @strict: true +//// declare function foo(): number | undefined +//// let x; +//// x = foo(); +//// let [|y2|] = [|x|]!; + +const [afterAssertion, beforeAssertion] = test.ranges() + +verify.typeAtLocation(beforeAssertion, "number | undefined") +verify.typeAtLocation(afterAssertion, "number")