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

Type narrowing is not allowed if variable is reassigned after the use site inside if block #1764

Closed
vladima opened this issue Jan 21, 2015 · 6 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@vladima
Copy link
Contributor

vladima commented Jan 21, 2015

Current implementation is very conservative when looking for assignments during application of type narrowing.

function len(x: number | string) {
     if (typeof x === 'number') {
         var t = x.toExponential();
         //x = "aaaa"; // uncomment me
     }
}

variable is reassigned after the usage of narrowed type but type narrowing still not kicked in

@danquirk danquirk added the Suggestion An idea for TypeScript label Jan 21, 2015
@ahejlsberg
Copy link
Member

We'd have to do flow analysis to properly reason about this. That might be worthwhile (and would allow us to detect unreachable code, unassigned variables, etc. as well), but we need to consider the impact on complexity and performance.

@vladima
Copy link
Contributor Author

vladima commented Jan 21, 2015

btw, there is already a PR for control flow analysis: #1287

@jbondc
Copy link
Contributor

jbondc commented Feb 4, 2015

Control flow looks really nice 👍

@mhegazy mhegazy added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Dec 8, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2015

So what is the proposal?

@ivogabe
Copy link
Contributor

ivogabe commented Feb 7, 2016

I've opened a PR #6959 that will fix this.

@ivogabe
Copy link
Contributor

ivogabe commented Jul 13, 2016

I think that this can be closed, #8010

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants