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
classEither<T>{publicstaticLeft<T>(value: T): Either<T>{returnnewEither('left',value);}publicstaticRight<T>(value: T): Either<T>{returnnewEither('right',value);}privateconstructor(publicreadonlykind: 'left'|'right',publicreadonlyvalue: T){}}functiondoSomething(): string{conste=Either.Left('foo');// e.kind: 'left' | 'right';switch(e.kind){case'left':
returne.value;case'right':
returne.value;// case 'foo': as expected, this doesn't work// break;default:
const_exhaustiveCheck: never=e;// even though we've matched against all the possible cases of `kind`, compiler still complains herethrownewError("How did we get here?");}}
TypeScript Version: Nightly (v4.1.0-dev.20200807)
Search Terms: Tagged union, discriminated union, exhaustive checking
Code
TypeScript Playground Example
However this can be made to work if we did this,
Playground
Now doing
switch (e.kind)
would perform the right checks.Expected behavior:
Exhaustive type checking should work in the case with the
class Either
andkind: 'left' | 'right'
.Actual behavior:
Exhaustive type checking fails even though compiler understand the union only has 2 fields (e.g.
case 'foo'
is an error).The text was updated successfully, but these errors were encountered: