-
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
Type guards should be flowed #10734
Comments
The request here is to infer user defined type guards from return expression of functions. i.e.: function isNumber(a): a is number {
....
}
function myIsNumber(a) {
return isNumber(a); // should infer `a is number`
}
function isString(a: number | string) {
return !isNumber(a); // should infer `a is string`
} |
I wish this issue got more attention because a bit of inference could go a long way. I implemented a form of type predicate inference in 982c35f to get a feeling for the complexity. It goes beyond just propagating type predicates as outlined here and infers them as requested in #18572 and #19468. It's only a sketch and does not handle some common cases nor some of the edge cases I saw in other places that do type narrowing. That notwithstanding, I think the (implementation) complexity of an inference scheme for simple arrow types might be acceptable. However, I obviously lack the broader understanding necessary to properly assess this. I want to emphasize how valuable an inference scheme like this could be. Manually specified predicates are not verified and thus error prone. The most common use case, as can be seen in #18572 and #19468, are Once again, I wish this issue would get more attention. =) |
Seems like a duplicate of #16069 |
Tracking at #16069 |
Similar issues: #7657, #5101, #5951
The text was updated successfully, but these errors were encountered: