ts-pattern
ts-pattern copied to clipboard
Exhaustive fails when matching an enum
Describe the bug
.exhaustive
returns a false negative type error when matching an enum.
Code Sandbox with a minimal reproduction case https://codesandbox.io/s/focused-chatterjee-exp2m
Versions
- TypeScript version: 4.3.2
- ts-pattern version: 3.3.3
Thanks for reporting, I'll look into it.
In the meantime, it looks like it works fine with named enums, so here is a workaround:
import { match } from "ts-pattern";
enum Test {
One = "One",
Two = "Two"
}
const test = Test.One as Test;
match(test)
.with(Test.One, () => "one")
.with(Test.Two, () => "two")
.exhaustive();
This looks like a TypeScript bug, I opened an issue on their repo: https://github.com/microsoft/TypeScript/issues/46562
This should probably be closed or something, since the TypeScript team decided that this is the correct behavior... :facepalm:
Closing this thread since this is expected behavior