-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
No narrowing on condition captured by const variable #39996
Labels
Duplicate
An existing issue was already created
Comments
My apologies if this issue already exists, I couldn't find it with my search terms! Thanks for your help! |
Duplicate of #12184. |
You can use a type guard instead: function isNullish<T>(x: T | null | undefined): x is null | undefined {
return x == null;
}
declare const x: number | undefined;
if (!isNullish(x)) {
x // number
} |
DanielRosenwasser
changed the title
Derived boolean null check
No narrowing on condition captured by const variable
Aug 13, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 4.0.0-beta
Search Terms:
Expected behavior:
When a created variable contains a null check, using that boolean should be sufficient for safe access to the variable.
Actual behavior:
TypeScript needs an additional null check.
Related Issues:
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: