Skip to content

Allow Not-null assertion fallback to assume initialize #42478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23414,9 +23414,11 @@ namespace ts {
node.parent.kind === SyntaxKind.NonNullExpression ||
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||
declaration.flags & NodeFlags.Ambient;
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
type === autoType || type === autoArrayType ? undefinedType :
getOptionalType(type);
const initialType = assumeInitialized
? isParameter
? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration)
: (type === autoType || type === autoArrayType ? undefinedType : type)
: (type === autoType || type === autoArrayType ? undefinedType : getOptionalType(type));
const flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized);
// 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
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/initializerWithNonNullAssertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [initializerWithNonNullAssertion.ts]
// https://github.com/microsoft/TypeScript/issues/19577

function test(numbers: number[]) {
let last;

for (const n of numbers) {
if (n % 2) {
return n;
}

last = n;
}

return last!;
}


//// [initializerWithNonNullAssertion.js]
// https://github.com/microsoft/TypeScript/issues/19577
function test(numbers) {
var last;
for (var _i = 0, numbers_1 = numbers; _i < numbers_1.length; _i++) {
var n = numbers_1[_i];
if (n % 2) {
return n;
}
last = n;
}
return last;
}
30 changes: 30 additions & 0 deletions tests/baselines/reference/initializerWithNonNullAssertion.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/initializerWithNonNullAssertion.ts ===
// https://github.com/microsoft/TypeScript/issues/19577

function test(numbers: number[]) {
>test : Symbol(test, Decl(initializerWithNonNullAssertion.ts, 0, 0))
>numbers : Symbol(numbers, Decl(initializerWithNonNullAssertion.ts, 2, 14))

let last;
>last : Symbol(last, Decl(initializerWithNonNullAssertion.ts, 3, 7))

for (const n of numbers) {
>n : Symbol(n, Decl(initializerWithNonNullAssertion.ts, 5, 14))
>numbers : Symbol(numbers, Decl(initializerWithNonNullAssertion.ts, 2, 14))

if (n % 2) {
>n : Symbol(n, Decl(initializerWithNonNullAssertion.ts, 5, 14))

return n;
>n : Symbol(n, Decl(initializerWithNonNullAssertion.ts, 5, 14))
}

last = n;
>last : Symbol(last, Decl(initializerWithNonNullAssertion.ts, 3, 7))
>n : Symbol(n, Decl(initializerWithNonNullAssertion.ts, 5, 14))
}

return last!;
>last : Symbol(last, Decl(initializerWithNonNullAssertion.ts, 3, 7))
}

34 changes: 34 additions & 0 deletions tests/baselines/reference/initializerWithNonNullAssertion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== tests/cases/compiler/initializerWithNonNullAssertion.ts ===
// https://github.com/microsoft/TypeScript/issues/19577

function test(numbers: number[]) {
>test : (numbers: number[]) => number
>numbers : number[]

let last;
>last : any

for (const n of numbers) {
>n : number
>numbers : number[]

if (n % 2) {
>n % 2 : number
>n : number
>2 : 2

return n;
>n : number
}

last = n;
>last = n : number
>last : any
>n : number
}

return last!;
>last! : number
>last : number
}

16 changes: 16 additions & 0 deletions tests/cases/compiler/initializerWithNonNullAssertion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @noImplicitAny: true
// https://github.com/microsoft/TypeScript/issues/19577

function test(numbers: number[]) {
let last;

for (const n of numbers) {
if (n % 2) {
return n;
}

last = n;
}

return last!;
}