TypeScript Version: 2.3.4
Code
/*tsconfig.json{ "compilerOptions": { "target": "ES2015", "module": "commonjs", "strict": true }}*/letmaybeNumber=Math.random()>0.5 ? 2 : null;letones=[1,1,1];lettwoes;if(maybeNumber){twoes=ones.map(x=>x*maybeNumber);}console.log(maybeNumber,twoes);Expected behavior:
Compilation passes, TS should know that .map function will be executed immediately and so maybeNumber will not be null
Actual behavior:
Compilation error: error TS2531: Object is possibly 'null'
Weird:
When I change let maybeNumber to const maybeNumber compilation passes for some reason, even though type of maybeNumber should be still number | null
TypeScript Version: 2.3.4
Code
Expected behavior:
Compilation passes, TS should know that .map function will be executed immediately and so
maybeNumberwill not benullActual behavior:
Compilation error:
error TS2531: Object is possibly 'null'Weird:
When I change
let maybeNumbertoconst maybeNumbercompilation passes for some reason, even though type ofmaybeNumbershould be stillnumber | null