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

execution flow analysis disregards narrowing for primitives constants #7768

Closed
zpdDG4gta8XKpMCd opened this issue Apr 1, 2016 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@zpdDG4gta8XKpMCd
Copy link

nightly build

const x = Math.random() > 0.5 ? 'hey' : 1;
if (typeof x === 'number') {
    [].map(() => x + 2); // expected should typecheck, actual no-no-no
}

numbers can't mutate, x holds a number, also it is a constant, constant can't change its value, so ...

@DanielRosenwasser
Copy link
Member

My guess is that we stop trying to track the way this was narrowed because you're in an arrow function. Being able to track this would be pretty difficult, but maybe we could say that we still narrow in function expressions/arrow functions.

@DanielRosenwasser DanielRosenwasser added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Apr 1, 2016
@RyanCavanaugh
Copy link
Member

If we see a const narrowed by typeof, that should be "sticky" even in function bodies.

This isn't true for let, e.g.

let x = Math.random() > 0.5 ? 'hey' : 1;
if (typeof x === 'number') {
  // Unsafe
  window.setImmediate(() => x.toFixed());
  // Safe but not distinguishable from above
  [0].map(() => x.toFixed());
}
x = 'oops';

but we can be sure the typeof a const cannot change.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 28, 2016

duplicate of #7662.

@mhegazy mhegazy closed this as completed Apr 28, 2016
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Apr 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants