gvergnaud / ts-pattern

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`exhaustive` doesn't work when the conditions use an enum with numeric values

alcuadrado opened this issue · comments

Looks like a duplicate of #168.

See also #58 and #162.

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();