Skip to content
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

Strict mode: Object is possibly 'null' when it really isn't #16285

Closed
zaggino opened this issue Jun 6, 2017 · 2 comments · Fixed by #56908
Closed

Strict mode: Object is possibly 'null' when it really isn't #16285

zaggino opened this issue Jun 6, 2017 · 2 comments · Fixed by #56908
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@zaggino
Copy link

zaggino commented Jun 6, 2017

TypeScript Version: 2.3.4

Code

/*
tsconfig.json
{
  "compilerOptions": {
    "target": "ES2015",
    "module": "commonjs",
    "strict": true
  }
}
*/

let maybeNumber = Math.random() > 0.5 ? 2 : null;
let ones = [1, 1, 1];
let twoes;
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

@DanielRosenwasser
Copy link
Member

See #9998.

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 6, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants