`exhaustive` doesn't work when the conditions use an enum with numeric values
alcuadrado opened this issue · comments
Describe the bug
If you try to match over a type who's discriminating field is an enum
with numeric values, exhaustive()
seems to get lost and produces a type error when it's not needed.
Workaround
Defining the enum values as string
fixes the problem. Defining them as number
(explicitly), doesn't.
Versions
- TypeScript version: 5.1.6
- ts-pattern version: 5.0.4
- environment: node 20.5.0, ts's playground, and anything I tried.
Somewhat offtopic
This library is impressive! Thanks for building it.
Looks like a duplicate of #168.
Link to underlying ts bug/issue/defect/whatever microsoft/TypeScript#46562
Thanks! That's correct.
What about adding a "Limitations" section to the readme? This is really surprising.
I understand it's a TS limitation, but it must be hitting many (potential) users.
if you can't simply migrate to a string enum, there's a "dirty" (typesafe) workaround using numbers, just explicitly cast the enum value
match(t)
.with({ type: ThingType.FIRST as 0 }, f => console.log("first"))
.with({ type: ThingType.SECOND as 1 }, f => console.log("second"))
.exhaustive();