-
-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.with and Pattern.not error since 4.1.3 #138
Comments
Thanks definitely a bug, I'll take a look shortly |
I had a look and it turns out it's incredibly difficult to solve without breaking exhaustive checking for not patterns. It would be much simpler if TypeScript had Negated Types but it unfortunately doesn't. In the meantime, the best workaround is to use import { match, Pattern } from "ts-pattern";
type Input = { type: "video"; seconds: number };
let input = ({ type: "user", name: "Gabriel" } as unknown) as Input;
const output = match(input)
.with({ type: "video", seconds: 10 }, () => "video of 10 seconds.")
// Swapped these 👆👇
.with(
{ type: "video", seconds: Pattern.not(10) },
() => "not video of 10 seconds."
)
.otherwise(() => "something else"); |
Ah... That's a shame. The replication is a very simple example of my real code, which looks like this:
Any idea how to replace |
Yes, you could use |
Ah yes, of course! Thank you. Maybe it would make sense to update the docs for |
Hey, I found a way to fix this issue in the end. It's not perfect but it solve your use case, see details in the release note: https://github.com/gvergnaud/ts-pattern/releases/tag/v4.1.4 |
thx for fixing this, sucks that negated types can't be used but it's cool to see this library develop -- I hope for the day we get rust-esque exhaustive |
Describe the bug
Since 4.1.3, when using
Pattern.not
, the following branches will act as ifPattern.not
was in factPattern.any
. If narrowing in following branches cannot be done, then I would expect the contrary (no narrowing onPattern.not
usages).Code Sandbox with a minimal reproduction case
https://codesandbox.io/s/typescript-playground-export-forked-48dyon?file=/index.ts
Versions
Thanks in advance!
The text was updated successfully, but these errors were encountered: