Idea: introduce tuple types #2196
Replies: 10 comments
-
Is the It is also interesting to know whether there are any thoughts on arity constraints on the tuple type. The question raises from TypeScript's limitations regarding generics+tuples. Related: TypeScript issue on GitHub and StackOverflow describing a work around). I first thought of this issue as a [good-first-issue]. But now I'm more like 😅 => 😓. |
Beta Was this translation helpful? Give feedback.
-
After thinking a little bit about it, I realize that there might be a compromise solution if it aligns with your overall vision for the library's API surface. The class types {
static tuple<TTuple extends Array<TItem>, TItem>(initialValue: TTuple): { value: TTuple } {
return { value: initialValue };
}
} The usage will either require an actual tuple (defined as such) instance to be provided as initial value: const rawTuple: [string, number] = ['my tuple', 42];
const byInference = types.tuple(rawTuple); or an array value can be provided instead, but the hint should be given for the type to be inferred as a tuple: const byHint = types.tuple(['my tuple', 42] as [string, number]); Unfortunately, if the array is not defined as tuple and hint is not given, the type will be inferred as Just my two cents... :) |
Beta Was this translation helpful? Give feedback.
-
requires TS 3.1 though |
Beta Was this translation helpful? Give feedback.
-
of course it would be a bit more complicated than that for the instance part, but that's the core idea :) |
Beta Was this translation helpful? Give feedback.
-
I picked up this issue yesterday. I am toying around with it, based on @xaviergonz and @another-guy previous comments, for now but if people are still interested I can try and formalise it a bit more. |
Beta Was this translation helpful? Give feedback.
-
I've run into a situation where this would be useful. I want to model a literal array of several model types. I think this should be possible now with newer TS feature but I'm not experienced enough with TS yet to sort out exactly how. The SO answer linked above has been updated with an example using newer TS features that might work in this case. Has anybody explored this now that TS seems to be more capable? |
Beta Was this translation helpful? Give feedback.
-
Any update? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Any updates? Tuple is really useful |
Beta Was this translation helpful? Give feedback.
-
Hey folks, This issue is a little stale, and I think @Yurchishin has the right idea here. I'm going to convert this to a discussion, mark that comment as "the answer" in case anyone wants a quick custom type to use, and if anyone out there wants to put together a PR with tests and documentation, I'd be happy to review it. Thanks, all! |
Beta Was this translation helpful? Give feedback.
-
like:
types.tuple(Car, types.string, types.boolean)
(serialized / stored as array)Beta Was this translation helpful? Give feedback.
All reactions