Skip to content

Commit 2f65b2c

Browse files
HerringtonDarkholmemhegazy
authored andcommitted
fix #11463, null assertion block uninitialized error (#14020)
1 parent 5ef6192 commit 2f65b2c

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12035,6 +12035,7 @@ namespace ts {
1203512035
// declaration container are the same).
1203612036
const assumeInitialized = isParameter || isOuterVariable ||
1203712037
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
12038+
node.parent.kind === SyntaxKind.NonNullExpression ||
1203812039
isInAmbientContext(declaration);
1203912040
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, getRootDeclaration(declaration) as VariableLikeDeclaration) : type) :
1204012041
type === autoType || type === autoArrayType ? undefinedType :

tests/baselines/reference/typeGuardsAsAssertions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ function f6() {
119119
x = <string | null>"";
120120
x!.slice();
121121
}
122+
123+
function f7() {
124+
let x: string;
125+
x!.slice();
126+
}
122127

123128

124129
//// [typeGuardsAsAssertions.js]
@@ -227,3 +232,7 @@ function f6() {
227232
x = "";
228233
x.slice();
229234
}
235+
function f7() {
236+
var x;
237+
x.slice();
238+
}

tests/baselines/reference/typeGuardsAsAssertions.symbols

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,15 @@ function f6() {
312312
>slice : Symbol(String.slice, Decl(lib.d.ts, --, --))
313313
}
314314

315+
function f7() {
316+
>f7 : Symbol(f7, Decl(typeGuardsAsAssertions.ts, 119, 1))
317+
318+
let x: string;
319+
>x : Symbol(x, Decl(typeGuardsAsAssertions.ts, 122, 7))
320+
321+
x!.slice();
322+
>x!.slice : Symbol(String.slice, Decl(lib.d.ts, --, --))
323+
>x : Symbol(x, Decl(typeGuardsAsAssertions.ts, 122, 7))
324+
>slice : Symbol(String.slice, Decl(lib.d.ts, --, --))
325+
}
326+

tests/baselines/reference/typeGuardsAsAssertions.types

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,17 @@ function f6() {
382382
>slice : (start?: number | undefined, end?: number | undefined) => string
383383
}
384384

385+
function f7() {
386+
>f7 : () => void
387+
388+
let x: string;
389+
>x : string
390+
391+
x!.slice();
392+
>x!.slice() : string
393+
>x!.slice : (start?: number | undefined, end?: number | undefined) => string
394+
>x! : string
395+
>x : string
396+
>slice : (start?: number | undefined, end?: number | undefined) => string
397+
}
398+

tests/cases/conformance/controlFlow/typeGuardsAsAssertions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,8 @@ function f6() {
120120
x = <string | null>"";
121121
x!.slice();
122122
}
123+
124+
function f7() {
125+
let x: string;
126+
x!.slice();
127+
}

0 commit comments

Comments
 (0)