Skip to content
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

Closed
hesxenon opened this issue Dec 8, 2021 · 4 comments
Closed

select in array/tuple/list infers wrong type #62

hesxenon opened this issue Dec 8, 2021 · 4 comments

Comments

@hesxenon
Copy link

hesxenon commented Dec 8, 2021

Describe the bug
when using select in a .with clause the inferred type is Array<T> instead of T

Code Sandbox with a minimal reproduction case
https://codesandbox.io/s/practical-moore-ytmpf?file=/src/index.ts

Versions

  • TypeScript version: 4.4.4
  • ts-pattern version: 3.3.3
  • environment: chromium 96 / node 17
@gvergnaud
Copy link
Owner

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.

@hesxenon
Copy link
Author

hesxenon commented Dec 9, 2021

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]) => {})

?

@gvergnaud
Copy link
Owner

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 [subpattern] pattern for unary tuples.

@hesxenon
Copy link
Author

hesxenon commented Dec 22, 2021

hmmmm, I see your point... but the correct type could be inferred with the distinction of readonly arrays, right?

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 noUncheckedIndexedAccess...


(P.s.: sorry for the late answer, have stopped getting notifications from github for some reason)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants