You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I then use this to create a throw statement at the end of a switch that will type-error if I expanded the union that went into the switch and neglected to implement a branch that handles the new subtype. This allows me to perform a run-time assertion of an unsupported option and check that I have implemented all supported options in one line.
switch(union.discriminant){case"foo": returndoThis();case"bar": returndoThat();default:
throwError(unreachable(union,"Unrecognized union type."));
It works beautifully until the union is not not actually a union at all (see case c in the example above). This ruined my ability to keep the switch form plus unreachable generically regardless of whether the switch argument is a discriminated union or just a simple object. The thing is, I run into situations all the time where the switch will first art with one discriminant value and grow later. But at any point in time I want to be able to capture both the run-time error and the compile-time check that all valid options have been exhausted. This includes when there is only one case on the switch.
Therefore I would find it useful that the behavior between c and b above are consistent. That way, I can use the switch boilerplate starting with the non-union and then expand it as I go.
Related Issues:
This seems to be related (but still somewhat different) to #20375 and #30557. I'm fine if this is unified into an existing issue as the essence of this issue remains addressed.
The text was updated successfully, but these errors were encountered:
soul-codes
changed the title
Inconsistency of narrowing behavior between constant and union
Inconsistency in narrowing behavior between non-union and union
Sep 30, 2019
TypeScript Version: 3.7.0-dev.20190930
Search Terms: union never switch
Code
Compare the type of the
switch
argument at thedefault:
branch of the threeswitch
es below:Expected behavior:
c
should benever
to be consistent witha
andb
.Actual behavior:
c
is still the type{ discriminant: "foo" }
.Playground Link: here
Context
I have this type-guarding function that acts as an identity function on the second argument but requires that the first argument is
never
.I then use this to create a
throw
statement at the end of aswitch
that will type-error if I expanded the union that went into the switch and neglected to implement a branch that handles the new subtype. This allows me to perform a run-time assertion of an unsupported option and check that I have implemented all supported options in one line.It works beautifully until the union is not not actually a union at all (see case
c
in the example above). This ruined my ability to keep theswitch
form plusunreachable
generically regardless of whether the switch argument is a discriminated union or just a simple object. The thing is, I run into situations all the time where the switch will first art with one discriminant value and grow later. But at any point in time I want to be able to capture both the run-time error and the compile-time check that all valid options have been exhausted. This includes when there is only one case on theswitch
.Therefore I would find it useful that the behavior between
c
andb
above are consistent. That way, I can use the switch boilerplate starting with the non-union and then expand it as I go.Related Issues:
This seems to be related (but still somewhat different) to #20375 and #30557. I'm fine if this is unified into an existing issue as the essence of this issue remains addressed.
The text was updated successfully, but these errors were encountered: