-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggestion: is not typegaurd #9378
Comments
I think you want #1809 because then you could just write type Defined = string | number | boolean | object;
function defined(value: any): value is Defined { return value != null; } |
Oh yeah, that would work nicely. Any reason the following wouldn't work? type Defined = string | number | boolean | {};
function defined(value: any): value is Defined { return value != null; } I thought the "{}" type was effectively "object", but the link you pointed me at seems to suggest otherwise. |
Or if I understand correctly, perhaps I can just use the function defined(value: any): value is {} { return value != null; } About to try this. |
you can achieve this today using generic types, as such: function defined<T>(value: T | undefined | null): value is T { return value != null; } |
@mhegazy beat me to it, I was about to post function isDefined<T>(x: T | undefined | null): x is T {
return x != undefined;
} |
Thanks guys, all of these solutions work great, including |
To support the following use case, for example:
This would be particularly useful when combined with strictNullChecks:
The text was updated successfully, but these errors were encountered: