From 2d2117237cc51857c83618267de35489765b08c3 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Sat, 11 Feb 2017 23:38:17 +0800 Subject: [PATCH] fix #11463, null assertion block uninitialized error --- src/compiler/checker.ts | 2 +- .../baselines/reference/typeGuardsAsAssertions.js | 9 +++++++++ .../reference/typeGuardsAsAssertions.symbols | 12 ++++++++++++ .../reference/typeGuardsAsAssertions.types | 14 ++++++++++++++ .../controlFlow/typeGuardsAsAssertions.ts | 5 +++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ac1135788cd1c..97fda44fb3686 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10582,7 +10582,7 @@ namespace ts { // declaration container are the same). const assumeInitialized = isParameter || isOuterVariable || type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node)) || - isInAmbientContext(declaration); + node.parent.kind === SyntaxKind.NonNullExpression || isInAmbientContext(declaration); const flowType = getFlowTypeOfReference(node, type, assumeInitialized, 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 diff --git a/tests/baselines/reference/typeGuardsAsAssertions.js b/tests/baselines/reference/typeGuardsAsAssertions.js index 84fc996c3ab50..1a59672df77bf 100644 --- a/tests/baselines/reference/typeGuardsAsAssertions.js +++ b/tests/baselines/reference/typeGuardsAsAssertions.js @@ -120,6 +120,11 @@ function f6() { x = ""; x!.slice(); } + +function f7() { + let x: string; + x!.slice(); +} //// [typeGuardsAsAssertions.js] @@ -227,3 +232,7 @@ function f6() { x = ""; x.slice(); } +function f7() { + var x; + x.slice(); +} diff --git a/tests/baselines/reference/typeGuardsAsAssertions.symbols b/tests/baselines/reference/typeGuardsAsAssertions.symbols index 5079f3b8cb44a..63aa4f1b01747 100644 --- a/tests/baselines/reference/typeGuardsAsAssertions.symbols +++ b/tests/baselines/reference/typeGuardsAsAssertions.symbols @@ -313,3 +313,15 @@ function f6() { >slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) } +function f7() { +>f7 : Symbol(f7, Decl(typeGuardsAsAssertions.ts, 120, 1)) + + let x: string; +>x : Symbol(x, Decl(typeGuardsAsAssertions.ts, 123, 7)) + + x!.slice(); +>x!.slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardsAsAssertions.ts, 123, 7)) +>slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/typeGuardsAsAssertions.types b/tests/baselines/reference/typeGuardsAsAssertions.types index 06dda2691be8e..2ef1e91430193 100644 --- a/tests/baselines/reference/typeGuardsAsAssertions.types +++ b/tests/baselines/reference/typeGuardsAsAssertions.types @@ -383,3 +383,17 @@ function f6() { >slice : (start?: number | undefined, end?: number | undefined) => string } +function f7() { +>f7 : () => void + + let x: string; +>x : string + + x!.slice(); +>x!.slice() : string +>x!.slice : (start?: number | undefined, end?: number | undefined) => string +>x! : string +>x : string +>slice : (start?: number | undefined, end?: number | undefined) => string +} + diff --git a/tests/cases/conformance/controlFlow/typeGuardsAsAssertions.ts b/tests/cases/conformance/controlFlow/typeGuardsAsAssertions.ts index 912eaa64e80de..5ce430bc19a6d 100644 --- a/tests/cases/conformance/controlFlow/typeGuardsAsAssertions.ts +++ b/tests/cases/conformance/controlFlow/typeGuardsAsAssertions.ts @@ -120,3 +120,8 @@ function f6() { x = ""; x!.slice(); } + +function f7() { + let x: string; + x!.slice(); +}