diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8551652c176d5..9ce44254bbcfe 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12035,6 +12035,7 @@ namespace ts { // declaration container are the same). const assumeInitialized = isParameter || isOuterVariable || type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) || + node.parent.kind === SyntaxKind.NonNullExpression || isInAmbientContext(declaration); const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, getRootDeclaration(declaration) as VariableLikeDeclaration) : type) : type === autoType || type === autoArrayType ? undefinedType : diff --git a/tests/baselines/reference/typeGuardsAsAssertions.js b/tests/baselines/reference/typeGuardsAsAssertions.js index 8c9b2048514aa..692af453c0cea 100644 --- a/tests/baselines/reference/typeGuardsAsAssertions.js +++ b/tests/baselines/reference/typeGuardsAsAssertions.js @@ -119,6 +119,11 @@ function f6() { x = <string | null>""; 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 71c02412a12ea..346418871db0a 100644 --- a/tests/baselines/reference/typeGuardsAsAssertions.symbols +++ b/tests/baselines/reference/typeGuardsAsAssertions.symbols @@ -312,3 +312,15 @@ function f6() { >slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) } +function f7() { +>f7 : Symbol(f7, Decl(typeGuardsAsAssertions.ts, 119, 1)) + + let x: string; +>x : Symbol(x, Decl(typeGuardsAsAssertions.ts, 122, 7)) + + x!.slice(); +>x!.slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardsAsAssertions.ts, 122, 7)) +>slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/typeGuardsAsAssertions.types b/tests/baselines/reference/typeGuardsAsAssertions.types index 09378bcee54ab..bd11b4a6f2057 100644 --- a/tests/baselines/reference/typeGuardsAsAssertions.types +++ b/tests/baselines/reference/typeGuardsAsAssertions.types @@ -382,3 +382,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 = <string | null>""; x!.slice(); } + +function f7() { + let x: string; + x!.slice(); +}