-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Narrow to Exclude
type in conditional code
#29280
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
Comments
This should be covered by #22348 |
Possibly a duplicate of #23764 |
Notice that this is consistent with the fact that the positive branch infers the |
#22348 would be what this needs, yeah. Conditional types still have a few problems that make the current behavior by-design, though 😦 |
I built #22348 locally and the example above fails to compile with the same error: |
PR isn't quite fully done (it has issues as-is). It doesn't swap control flow to using extract/exclude yet afaik. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue is likely a duplicate of #23764, but while that was a bug report (and thus closed as "not a bug"), this is a feature request, proposing an improvement to type narrowing. |
Search Terms
conditional else exclude narrow
Suggestion
TypeScript should infer
Exclude
types in conditional expressions and statements.Specifically, whenever the positive branch (
if
, or thex
inc ? x : y
) has a more specific type thanks to a type guard, the negative branch (else
/y
) should have the relevantExclude
type.Use Cases
This would provide a natural way to infer
Exclude
types and it would generalise in a natural way the current behaviour of conditional statements over union and finite-domain types.Apparently right now the only way to have a value of type
Exclude
is to assert it manually (either through type guards or type assertions).Examples
Playground link
Checklist
My suggestion meets these guidelines:
I am unsure if this could break existing code.
Making the typing more accurate could reveal dead code, which could result in compilation errors.
The text was updated successfully, but these errors were encountered: