-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
select in array/tuple/list infers wrong type #62
Comments
Hey, This behavior is expected, an array with a single sub pattern in it is actually a "list pattern" which will match arrays in which each item matches the subpattern. That's why the value you selected ends up being a list of values, there is one for each item in the array. For more info, see the doc section about list patterns: https://github.com/gvergnaud/ts-pattern#lists-arrays I know this is a bit confusing. List patterns might change in v4 to avoid the confusion with unary tuples. |
ok, makes sense (and I can definitely see a use-case for this) but especially since you mention tuples right below the linked docs I'd expect this to select e.g. only the first value as long as the pattern I'm matching against is a "tuple", i.e. a readonly array. Do you think it's possible to enable this like .with([0], (zeroes) => {}) and .with([0] as const, ([firstZero]) => {}) ? |
Unfortunately readonly arrays are only a thing at the type level, so there is no way for the runtime to know whether an array is readonly or not and branch on that information to have different behaviors. I think the best way to solve this is to have a different kind of pattern for lists and reserve the |
hmmmm, I see your point... but the correct type could be inferred with the distinction of As for the runtime, imho it's acceptable to specify how you want to match, i.e. I think additional api like .with([0], match.tuple(([firstZero]) => {})) would be totally fine. And it could even solve the typing problem as well? I mean I'd think the subpattern use case is probably less common than the tuple matching use case but yeah, just an opinion. Any thoughts on this? Oh, and also the tuple example in the docs won't work with (P.s.: sorry for the late answer, have stopped getting notifications from github for some reason) |
Describe the bug
when using
select
in a.with
clause the inferred type isArray<T>
instead ofT
Code Sandbox with a minimal reproduction case
https://codesandbox.io/s/practical-moore-ytmpf?file=/src/index.ts
Versions
The text was updated successfully, but these errors were encountered: