Variable isn't narrowed within a capturing closure · Issue #35124 · microsoft/TypeScript (original) (raw)
TypeScript 3.7.2
Playground link
Compiler Options:
{ "compilerOptions": { "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "useDefineForClassFields": false, "alwaysStrict": true, "allowUnreachableCode": false, "allowUnusedLabels": false, "downlevelIteration": false, "noEmitHelpers": false, "noLib": false, "noStrictGenericChecks": false, "noUnusedLocals": false, "noUnusedParameters": false, "esModuleInterop": true, "preserveConstEnums": false, "removeComments": false, "skipLibCheck": false, "checkJs": false, "allowJs": false, "experimentalDecorators": false, "emitDecoratorMetadata": false, "target": "ES2017", "module": "ESNext" } }
Input:
let i: number | undefined; i = 0; let j:number = i+1; // works (k: number) => k === i+1; // error: Object i is possibly undefined
Output:
"use strict"; let i; i = 0; let j = i + 1; // works (k) => k === i + 1; // error: Object i is possibly undefined
Expected behavior:
The compiler should not complain about the last i+1
because it's clearly a number
type after assigning 0
to it.
I suspect the closure uses the type of i
from the let
statement and ignores it being narrowed down later on. Is this expected behavior?