`Pattern.nonNullable` does not behave as expected
ItaiYosephi opened this issue · 2 comments
ItaiYosephi commented
Describe the bug
Pattern.nonNullable
behave the same Pattern.not(Pattern.nullish)
Code Sandbox with a minimal reproduction case
https://codesandbox.io/p/sandbox/thirsty-frog-9965h3?file=%2Fsrc%2Findex.ts%3A20%2C14
provided this code
enum AnimalType {
Dog = "dog",
Cat = "cat",
}
interface Animal {
type?: AnimalType;
name: string;
}
const animal: Animal = { name: "Bobbie" };
match(animal)
.with(
// replace the next line with:
// { type: Pattern.not(Pattern.nullish) },
{ type: Pattern.nonNullable },
({ name, type }) => `${name} the ${type}`
)
.with({ type: Pattern.optional(Pattern.nullish) }, ({ name }) => name)
.exhaustive();
match(animal)
.with(
{ type: Pattern.not(Pattern.nullish) },
{ type: Pattern.nonNullable },
({ name, type }) => `${name} the ${type}`
)
// without `Optional` - there's no TS error, but there is runtime error
.with({ type: Pattern.nullish }, ({ name }) => name)
.exhaustive();
Versions
- TypeScript version: 5.4.3
- ts-pattern version: 5.1.1
- environment: browser
gvergnaud commented
That's indeed a bug. I'll take a closer look when I get some time. In the meantime using P.not(P.nullish)
instead of P.nonNullable
is the alternative I'd recommend if you run into this issue
gvergnaud commented
Fixed in 5.1.2!