-
this is my Error:
this is my code:
what can I do to fix this? |
Beta Was this translation helpful? Give feedback.
Answered by
gvergnaud
Mar 2, 2024
Replies: 1 comment
-
Make sure import {match} from 'ts-pattern'
class ErrorA extends Error {
_type = "A" as const
}
class ErrorB extends Error {
_type = "B" as const
}
class ErrorC extends Error {
_type = "C" as const
}
const input = new ErrorA as ErrorA | ErrorB | ErrorC
const res = match(input)
.with({_type: 'A' }, () => {})
.with({_type: 'B' }, () => {})
.with({_type: 'C' }, () => {})
.exhaustive() // ✅ works |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
rolfst
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure
_type
on your custom error is declared as string literal (you can useas const
for example)Playground