Syntax for explicit tuple literal type #48052
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
Though closely related to #10195 and #16656, I am opening this issue because the proposed syntax, purpose and demonstration are different.
Motivation
Previously I wrote a library that manipulates nested arrays:
But without tuple its type is being inferred as
NDArray<number>, of whichNDArrayis defined asThe Problem
I know there are at least 3 ways to cast it as a tuple:
of which the
tuplefunction is defined asIn these 3 cases, the program correctly infers the type of the results as
number[][].But these 3 ways all have their problems:
For 1, it is not necessary to make the numbers constants.
For 2, we will have to write
number,many times for higher-dimensional arrays (I know we can use a utility type, but I don’t think it’s desirable).For 3, there are considerable runtime effects and it might cause serious performance problems, especially when it is transpiled down to something like
Also, in a module-based application, this utility function will have to be defined or imported before using, which pollutes the namespace.
Most importantly, for 1 and 2, we are using the
askeywords, which is not favorable for TypeScript codes in general (and ESLint gets angry too).I also know the existence of the Record & Tuple Proposal, but since there will be compatibility problems in the near future, and people might not want to refactor the existing codes, the current tuple-like array will still be used by a significant amount of projects.
Solution
In my opinion, a syntax without
asliketuple [2, 3]should be created, or we should at least provide a way to let TypeScript infer a literal array as tuple for a certain function parameter.Prior Use
The tuple utility function is included in many repositories, and even in the release note:
The text was updated successfully, but these errors were encountered: