-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Exhaustiveness check does not work for tagged union when there is the only tag #38963
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
We've discussed narrowing-to-never of nonunions (singletons) in other issues and it's unfortunately not a great way forward. There are a few issues with doing this. The first is performance; for non-unions we simply don't do any narrowing logic like this in the first place. The second is that immediately going to function fn(arg: { x: string }) {
// Legacy wrapper
if (arg.x === undefined) {
// ??Error??, arg is of type never
arg.x = "";
}
} I don't have a super-great workaround off the top of my head that keeps the single-case |
Ok, thank you for detailed answer. I didn't realize there can exist examples like this because I was lucky enough to not see anything like that =) This in theory can be solved with another I suppose I should close this now, as this behaviour will not be changed. |
@RyanCavanaugh Doesn't discriminated-union narrowing only apply when the property being checked is a unit type, though? In your example |
TypeScript Version: 3.9.2
Search Terms:
Expected behavior:
No compile errors: we cannot get the
default
branch, so type ofx
in there should be narrowed down tonever
, and the functiontest
is correct.Actual behavior:
Compile error at line 20 (
return absurd(x);
):However, if we uncomment the code that adds the second item to Disjoint, it will be working properly.
Code
This may seem a weird corner-case, but I have stumbled this behaviour multiple times. You can have a tagged union of single tag when you plan to extend it later. When you want to ensure that later when you add new tags to it, you will get compile errors in every non-exhaustive switch statement that must be exhaustive.
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: