ts-pattern icon indicating copy to clipboard operation
ts-pattern copied to clipboard

Exhaustive fails when matching an enum

Open jakelazaroff opened this issue 3 years ago • 2 comments

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

jakelazaroff avatar Oct 27 '21 22:10 jakelazaroff

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

gvergnaud avatar Oct 28 '21 12:10 gvergnaud

This looks like a TypeScript bug, I opened an issue on their repo: https://github.com/microsoft/TypeScript/issues/46562

gvergnaud avatar Oct 28 '21 13:10 gvergnaud

This should probably be closed or something, since the TypeScript team decided that this is the correct behavior... :facepalm:

K4rakara avatar Oct 23 '22 02:10 K4rakara

Closing this thread since this is expected behavior

gvergnaud avatar Oct 24 '22 11:10 gvergnaud