-
import { match, P } from 'ts-pattern'
const merge = <A>(xs: A[], ys: A[]): A[] =>
match([xs, ys])
.with([P._, []], ([xs, _]) => xs)
.with([[], P._], ([_, ys]) => ys)
.with([[P._, ...P.array()], [P._, ...P.array()]], ([x, xs_, y, ys_]) =>
x <= y ? [x, ...merge(xs_, ys_)] : [y, ...merge(xs_, ys_)])
.exhaustive() Playground Link: Provided
|
Beta Was this translation helpful? Give feedback.
Answered by
gvergnaud
Sep 23, 2024
Replies: 1 comment
-
It's a known limitation that TS-Pattern doesn't handle matching on type parameters very well. That's because TypeScript inference gets stuck on unknown type parameters, and there is unfortunately nothing we can do about it in userland. You can work around this by using an overload for your merge function: Playground This also works, but is more brittle because it relies on implementation details of the way inference works (if you don't use parameters, their types dont get evaluated): Playground |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
scarf005
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a known limitation that TS-Pattern doesn't handle matching on type parameters very well. That's because TypeScript inference gets stuck on unknown type parameters, and there is unfortunately nothing we can do about it in userland.
You can work around this by using an overload for your merge function: Playground
This also works, but is more brittle because it relies on implementation details of the way inference works (if you don't use parameters, their types dont get evaluated): Playground