-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Unable to append a type to an existing tuple #31286
Comments
Your playground link links to an empty Playground. |
sorry about that, link in original post fixed. |
Duplicates #5453 and fixed in TS3.0 by #24897 via tuples in rest/spread positions: type Cons<H, T extends any[]> = ((h: H, ...t: T) => void) extends ((
...r: infer R
) => void)
? R
: never;
function combine<T, U extends any[] | [any]>(item: T, items: U): Cons<T, U> {
const ret = items.slice() as Cons<T, U>;
ret.unshift(item);
return ret;
}
const result = combine("hello", ["goodbye", 123, true]);
// const result: [string, string, number, boolean] Note: this should be called "prepend", not "append". Appending to and concatenating tuples is an open issue (#26058) with workarounds of various levels of hackiness available. |
That's an ...interesting way of fixing this! This is a hack though right? It's a very roundabout way of fixing the issue IMHO. Very appreciative of the solution though :-) |
It's a hack in that it's roundabout, but that's probably a separate issue #26113. But this definition of |
TypeScript Version: 3.2.4
Search Terms:
Tuple, merge, combine
Code
Expected behavior:
result should be typed as
[string, string, number, boolean]
.Actual behavior:
Compile error:
A rest element type must be an array type.
Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: