Narrowing Arrays to Tuples Using Match
Suppose we want to write a function that takes an array as input and does different things for certain concrete sizes. Is there a way to narrow from arrays to tuples with
My first thought would be to try something the following this using
Note that Python has native support for a similar-looking implementation using its built-in pattern matching:
---
As a note to the maintainers, a couple of proposals have surfaced in the ensuing thread:
- For homogeneous arrays, something like
- For heterogeneous arrays, a general-purpose
Match?My first thought would be to try something the following this using
Match, but the array type doesn't get narrowed correctly, so x and y are inferred to have type number | undefined, leading to a compilation error:Note that Python has native support for a similar-looking implementation using its built-in pattern matching:
---
- For homogeneous arrays, something like
Match.hasLength(2) could narrow from number[] to [number, number] by making use of the fact that number[] & { length: 2 } is equivalent to [number, number] in TypeScript.- For heterogeneous arrays, a general-purpose
Match.tuple utility supporting arbitrary tuples of refinements could be useful, similar to the Predicate.tuple utility that already exists for predicates.