-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow tuple and string types to be concat'ed. #16746
Comments
The idea came from this issue: #16389 |
Are you asking for an expression operator or a type operator? const x: [number, string] = [0, "0"];
const y: [number, string] = [1, "1"];
const z: [number, string, number, string] = [...x, ...y]; // Error (currently) But currently that would not be right because an array with 3 elements is assignable to a tuple with only 2 elements. See #6229. |
Well, that code should also compile, but it isn't quite what I want. The closest thing that I have currently found is to define a new tuple like this: I really don't know how much use there is for this operator in tuples, but I can see a LOT of possibilities for strings. But it seemed logical, and an easy way to prevent retyping any code and still keep type checking. Of course, this would be nice too: |
Me either ... tuples with more than 3 elements are generally a bad idea, so I can't see of much use for concatting tuples together. However, it would likely fall out of variadic function magic. function f<T>(...args: T): T { return args; }
f(...tuple1, ...tuple2);
Could you give some examples where it would be useful to add values with string literal types? |
Assuming you mean concat (instead of add), I am thinking of cases where you might have two options combining together into a string of selectors. This would be an easy way to combine options in a shopping cart for instance. type size = "Small" | "Medium" | "Large";
type color = "Red" | "Green" | "Blue";
const options: (size + color)[] = [];
//options will be an array of "SmallRed" | "SmallGreen" | "SmallBlue" | "MediumRed" | ... Of course, options usually get loaded from a database, but there are other use cases. Anywhere you want to hash a 2D array of options. |
Then that removes the need for a TupleRest variable. I think you're right. The most I usually use is 4 (never 10) and I mostly use them when I want to use the values without worrying about the variable name. |
Getting all combinations could be bad for performance. Would you write a switch statement with a case for every one of those? Or would you just have to parse it back anyway, in which case it might as well just be of type |
@andy-ms It might fall out the other way. It is already possible using type recursion to make "variadic" curried functions, like so: interface Pop<TVarStack extends VarStack> {
(a: TVarStack["head"]): Pop<TVarStack["tail"]>
}
interface VarStack<THead = any, TTail extends (void | VarStack) = any> {
head: THead
tail: TTail
<TNew>(a: TNew): VarStack<TNew, this>
(): (a: Pop<this>) => any
}
// Figure out implementation later :)
let stack: VarStack<void, void>;
const pop = stack(new Date())(10)("Bob's")("your")("uncle")()
// a, b, c, d, e are well-typed
pop(a => b => c => d => e => {
// Need this because can't figure out how to encode base-case in Pop recursion
return stack
}) There's some stuff I can't figure out, such as how to base-case the recursion, but you can see the foundations are there. What I really want is to transform my recursively nested type into an array type, instead of a function type as I'm doing here. Something like |
looks like this should be covered by #5453 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.3.4
Code
Feature request:
I would like to request a new way of combining tuples. The plus (
+
) operator. Currently this is the way strings are concatenated and numbers are summed. Therefore it seemed like a logical way to concatenate two tuple types. It should at least apply to Tuple and Array types, and the string type could definitely make use of it as well. For an array, it would be equivalent toArray<A | B>
.Also, the function
Array#concat
should make use of this operator if possible so that Tuples can easily be concat'ed in value as well as type.Current behavior when combining two tuple types using
&
:If we add more types, it becomes even more laborious at
t11
.The text was updated successfully, but these errors were encountered: