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 guard narrows type any in a primitive type check #1621

Merged
merged 1 commit into from
Jan 8, 2015

Conversation

ahejlsberg
Copy link
Member

Fixes #1595.

When a variable of type any appears in a typeof type guard that checks for one of the primtive types, the type of the variable is narrowed to that type. Type guards otherwise do not affect variables of type any.

Rationale: Since narrowing a variable of type any potentially introduces errors (because the type checking becomes stronger), we can only do so when we know for sure that the variable is exactly of the type that was checked for (and couldn't for example have additional properties)--and the only case that is true is a typeof check for a primitive type.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 8, 2015

👍

ahejlsberg added a commit that referenced this pull request Jan 8, 2015
Type guard narrows type any in a primitive type check
@ahejlsberg ahejlsberg merged commit 40dc134 into master Jan 8, 2015
@mhegazy mhegazy deleted the narrowingOfAny branch June 18, 2015 06:59
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

type guards are not working in this case
2 participants