Typescript: How to add an item to a tuple?
Why this produces an error "A rest element type must be an array type."?
type QWE<T extends [number, string]> = [boolean, ...T]
^^^^ the error is here
typescript typescript-typings
add a comment |
Why this produces an error "A rest element type must be an array type."?
type QWE<T extends [number, string]> = [boolean, ...T]
^^^^ the error is here
typescript typescript-typings
I'm guessing it has to do with the generic nature ofT, since evenT extends anyemits the same error.
– Madara Uchiha♦
Dec 31 '18 at 8:16
1
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:27
@TitianCernicova-Dragomir I just wanted to ask you to answer, but distracted to that answer aboutnever) Anyway, so there is no way to somehow modify tuples? No way to add something to the beginning or to the ending of a tuple? P.S. I would be happy to say my thanks as upvotes and selecting your answer
– Nurbol Alpysbayev
Dec 31 '18 at 8:30
At the beginning can be done by spreading the tuple to a function and extracting the arguments, I can provide it in an answer if you want. At the end I think there are some unrecommended hacks that involve recursive type aliases but they are explicitly not recomended
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:32
@TitianCernicova-Dragomir Yes, of course I want! Especially because I didn't quite understand the approach you wrote about :) Thanks in advance!
– Nurbol Alpysbayev
Dec 31 '18 at 8:33
add a comment |
Why this produces an error "A rest element type must be an array type."?
type QWE<T extends [number, string]> = [boolean, ...T]
^^^^ the error is here
typescript typescript-typings
Why this produces an error "A rest element type must be an array type."?
type QWE<T extends [number, string]> = [boolean, ...T]
^^^^ the error is here
typescript typescript-typings
typescript typescript-typings
asked Dec 31 '18 at 8:06
Nurbol AlpysbayevNurbol Alpysbayev
4,0961327
4,0961327
I'm guessing it has to do with the generic nature ofT, since evenT extends anyemits the same error.
– Madara Uchiha♦
Dec 31 '18 at 8:16
1
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:27
@TitianCernicova-Dragomir I just wanted to ask you to answer, but distracted to that answer aboutnever) Anyway, so there is no way to somehow modify tuples? No way to add something to the beginning or to the ending of a tuple? P.S. I would be happy to say my thanks as upvotes and selecting your answer
– Nurbol Alpysbayev
Dec 31 '18 at 8:30
At the beginning can be done by spreading the tuple to a function and extracting the arguments, I can provide it in an answer if you want. At the end I think there are some unrecommended hacks that involve recursive type aliases but they are explicitly not recomended
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:32
@TitianCernicova-Dragomir Yes, of course I want! Especially because I didn't quite understand the approach you wrote about :) Thanks in advance!
– Nurbol Alpysbayev
Dec 31 '18 at 8:33
add a comment |
I'm guessing it has to do with the generic nature ofT, since evenT extends anyemits the same error.
– Madara Uchiha♦
Dec 31 '18 at 8:16
1
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:27
@TitianCernicova-Dragomir I just wanted to ask you to answer, but distracted to that answer aboutnever) Anyway, so there is no way to somehow modify tuples? No way to add something to the beginning or to the ending of a tuple? P.S. I would be happy to say my thanks as upvotes and selecting your answer
– Nurbol Alpysbayev
Dec 31 '18 at 8:30
At the beginning can be done by spreading the tuple to a function and extracting the arguments, I can provide it in an answer if you want. At the end I think there are some unrecommended hacks that involve recursive type aliases but they are explicitly not recomended
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:32
@TitianCernicova-Dragomir Yes, of course I want! Especially because I didn't quite understand the approach you wrote about :) Thanks in advance!
– Nurbol Alpysbayev
Dec 31 '18 at 8:33
I'm guessing it has to do with the generic nature of
T, since even T extends any emits the same error.– Madara Uchiha♦
Dec 31 '18 at 8:16
I'm guessing it has to do with the generic nature of
T, since even T extends any emits the same error.– Madara Uchiha♦
Dec 31 '18 at 8:16
1
1
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:27
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:27
@TitianCernicova-Dragomir I just wanted to ask you to answer, but distracted to that answer about
never ) Anyway, so there is no way to somehow modify tuples? No way to add something to the beginning or to the ending of a tuple? P.S. I would be happy to say my thanks as upvotes and selecting your answer– Nurbol Alpysbayev
Dec 31 '18 at 8:30
@TitianCernicova-Dragomir I just wanted to ask you to answer, but distracted to that answer about
never ) Anyway, so there is no way to somehow modify tuples? No way to add something to the beginning or to the ending of a tuple? P.S. I would be happy to say my thanks as upvotes and selecting your answer– Nurbol Alpysbayev
Dec 31 '18 at 8:30
At the beginning can be done by spreading the tuple to a function and extracting the arguments, I can provide it in an answer if you want. At the end I think there are some unrecommended hacks that involve recursive type aliases but they are explicitly not recomended
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:32
At the beginning can be done by spreading the tuple to a function and extracting the arguments, I can provide it in an answer if you want. At the end I think there are some unrecommended hacks that involve recursive type aliases but they are explicitly not recomended
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:32
@TitianCernicova-Dragomir Yes, of course I want! Especially because I didn't quite understand the approach you wrote about :) Thanks in advance!
– Nurbol Alpysbayev
Dec 31 '18 at 8:33
@TitianCernicova-Dragomir Yes, of course I want! Especially because I didn't quite understand the approach you wrote about :) Thanks in advance!
– Nurbol Alpysbayev
Dec 31 '18 at 8:33
add a comment |
2 Answers
2
active
oldest
votes
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple, and I'm guessing implementing it would require significant effort and complexity.
To add a tuple at the end of another known tuple we can use the ability to spread a tuple tu a function and then extract the argument types as a tuple.
type ArgumentTypes<T extends (...a: any) => any> =
T extends (...a: infer A) => any ? A : never;
type QWE<T extends [number, string]> =
ArgumentTypes<(a: boolean, ...r: T) => void>
type R = QWE<[number, string]>
Adding the tuple at the start is more problematic, I believe there are some very unrecommended hacks that can achieve this using recursive type aliases. You can also define multiple conditions to support up to a number of elements in a tuple, but I would avoid it if possible
FYI there's built in helper typeParameters<T>so you can use it instead of definingArgumentTypes
– Aleksey L.
Dec 31 '18 at 13:43
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
add a comment |
If someone can find it useful, this is a version of Titian's type, with an improvement covering some more cases:
type R = TupleUnshift<boolean, [number, string]> // [boolean, number, string]
type TupleUnshift<A, B extends [...any]> = ((a: A, ...r: ForcedTuple<B>) => void) extends (...a: infer R) => any ? R : never
type ForcedTuple<T> =
T extends [
infer A,
infer B,
infer C,
infer D,
infer E,
infer F,
infer G,
infer H,
infer I,
infer J,
infer K,
infer L,
infer M,
infer N,
infer O,
infer P,
infer Q,
infer R,
infer S,
infer T,
infer U,
infer V,
infer W,
infer X,
infer Y,
infer Z
]
?
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
: T
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985074%2ftypescript-how-to-add-an-item-to-a-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple, and I'm guessing implementing it would require significant effort and complexity.
To add a tuple at the end of another known tuple we can use the ability to spread a tuple tu a function and then extract the argument types as a tuple.
type ArgumentTypes<T extends (...a: any) => any> =
T extends (...a: infer A) => any ? A : never;
type QWE<T extends [number, string]> =
ArgumentTypes<(a: boolean, ...r: T) => void>
type R = QWE<[number, string]>
Adding the tuple at the start is more problematic, I believe there are some very unrecommended hacks that can achieve this using recursive type aliases. You can also define multiple conditions to support up to a number of elements in a tuple, but I would avoid it if possible
FYI there's built in helper typeParameters<T>so you can use it instead of definingArgumentTypes
– Aleksey L.
Dec 31 '18 at 13:43
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
add a comment |
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple, and I'm guessing implementing it would require significant effort and complexity.
To add a tuple at the end of another known tuple we can use the ability to spread a tuple tu a function and then extract the argument types as a tuple.
type ArgumentTypes<T extends (...a: any) => any> =
T extends (...a: infer A) => any ? A : never;
type QWE<T extends [number, string]> =
ArgumentTypes<(a: boolean, ...r: T) => void>
type R = QWE<[number, string]>
Adding the tuple at the start is more problematic, I believe there are some very unrecommended hacks that can achieve this using recursive type aliases. You can also define multiple conditions to support up to a number of elements in a tuple, but I would avoid it if possible
FYI there's built in helper typeParameters<T>so you can use it instead of definingArgumentTypes
– Aleksey L.
Dec 31 '18 at 13:43
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
add a comment |
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple, and I'm guessing implementing it would require significant effort and complexity.
To add a tuple at the end of another known tuple we can use the ability to spread a tuple tu a function and then extract the argument types as a tuple.
type ArgumentTypes<T extends (...a: any) => any> =
T extends (...a: infer A) => any ? A : never;
type QWE<T extends [number, string]> =
ArgumentTypes<(a: boolean, ...r: T) => void>
type R = QWE<[number, string]>
Adding the tuple at the start is more problematic, I believe there are some very unrecommended hacks that can achieve this using recursive type aliases. You can also define multiple conditions to support up to a number of elements in a tuple, but I would avoid it if possible
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple, and I'm guessing implementing it would require significant effort and complexity.
To add a tuple at the end of another known tuple we can use the ability to spread a tuple tu a function and then extract the argument types as a tuple.
type ArgumentTypes<T extends (...a: any) => any> =
T extends (...a: infer A) => any ? A : never;
type QWE<T extends [number, string]> =
ArgumentTypes<(a: boolean, ...r: T) => void>
type R = QWE<[number, string]>
Adding the tuple at the start is more problematic, I believe there are some very unrecommended hacks that can achieve this using recursive type aliases. You can also define multiple conditions to support up to a number of elements in a tuple, but I would avoid it if possible
answered Dec 31 '18 at 8:38
Titian Cernicova-DragomirTitian Cernicova-Dragomir
63.1k33755
63.1k33755
FYI there's built in helper typeParameters<T>so you can use it instead of definingArgumentTypes
– Aleksey L.
Dec 31 '18 at 13:43
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
add a comment |
FYI there's built in helper typeParameters<T>so you can use it instead of definingArgumentTypes
– Aleksey L.
Dec 31 '18 at 13:43
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
FYI there's built in helper type
Parameters<T> so you can use it instead of defining ArgumentTypes– Aleksey L.
Dec 31 '18 at 13:43
FYI there's built in helper type
Parameters<T> so you can use it instead of defining ArgumentTypes– Aleksey L.
Dec 31 '18 at 13:43
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
@AlekseyL. 10x I was wondering why something like that that wasn't in lib.d.ts, guess I just missed it :)
– Titian Cernicova-Dragomir
Dec 31 '18 at 13:46
add a comment |
If someone can find it useful, this is a version of Titian's type, with an improvement covering some more cases:
type R = TupleUnshift<boolean, [number, string]> // [boolean, number, string]
type TupleUnshift<A, B extends [...any]> = ((a: A, ...r: ForcedTuple<B>) => void) extends (...a: infer R) => any ? R : never
type ForcedTuple<T> =
T extends [
infer A,
infer B,
infer C,
infer D,
infer E,
infer F,
infer G,
infer H,
infer I,
infer J,
infer K,
infer L,
infer M,
infer N,
infer O,
infer P,
infer Q,
infer R,
infer S,
infer T,
infer U,
infer V,
infer W,
infer X,
infer Y,
infer Z
]
?
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
: T
add a comment |
If someone can find it useful, this is a version of Titian's type, with an improvement covering some more cases:
type R = TupleUnshift<boolean, [number, string]> // [boolean, number, string]
type TupleUnshift<A, B extends [...any]> = ((a: A, ...r: ForcedTuple<B>) => void) extends (...a: infer R) => any ? R : never
type ForcedTuple<T> =
T extends [
infer A,
infer B,
infer C,
infer D,
infer E,
infer F,
infer G,
infer H,
infer I,
infer J,
infer K,
infer L,
infer M,
infer N,
infer O,
infer P,
infer Q,
infer R,
infer S,
infer T,
infer U,
infer V,
infer W,
infer X,
infer Y,
infer Z
]
?
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
: T
add a comment |
If someone can find it useful, this is a version of Titian's type, with an improvement covering some more cases:
type R = TupleUnshift<boolean, [number, string]> // [boolean, number, string]
type TupleUnshift<A, B extends [...any]> = ((a: A, ...r: ForcedTuple<B>) => void) extends (...a: infer R) => any ? R : never
type ForcedTuple<T> =
T extends [
infer A,
infer B,
infer C,
infer D,
infer E,
infer F,
infer G,
infer H,
infer I,
infer J,
infer K,
infer L,
infer M,
infer N,
infer O,
infer P,
infer Q,
infer R,
infer S,
infer T,
infer U,
infer V,
infer W,
infer X,
infer Y,
infer Z
]
?
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
: T
If someone can find it useful, this is a version of Titian's type, with an improvement covering some more cases:
type R = TupleUnshift<boolean, [number, string]> // [boolean, number, string]
type TupleUnshift<A, B extends [...any]> = ((a: A, ...r: ForcedTuple<B>) => void) extends (...a: infer R) => any ? R : never
type ForcedTuple<T> =
T extends [
infer A,
infer B,
infer C,
infer D,
infer E,
infer F,
infer G,
infer H,
infer I,
infer J,
infer K,
infer L,
infer M,
infer N,
infer O,
infer P,
infer Q,
infer R,
infer S,
infer T,
infer U,
infer V,
infer W,
infer X,
infer Y,
infer Z
]
?
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
: T
edited Jan 17 at 19:52
answered Dec 31 '18 at 9:04
Nurbol AlpysbayevNurbol Alpysbayev
4,0961327
4,0961327
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985074%2ftypescript-how-to-add-an-item-to-a-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm guessing it has to do with the generic nature of
T, since evenT extends anyemits the same error.– Madara Uchiha♦
Dec 31 '18 at 8:16
1
Before 3.0 we could not spread tuples or generic type arguments to a function and this had to be implemented. Similarly, rest in tuples currently only support arrays, nobody implemented the ability to spread other tuples into the rest of a given tuple
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:27
@TitianCernicova-Dragomir I just wanted to ask you to answer, but distracted to that answer about
never) Anyway, so there is no way to somehow modify tuples? No way to add something to the beginning or to the ending of a tuple? P.S. I would be happy to say my thanks as upvotes and selecting your answer– Nurbol Alpysbayev
Dec 31 '18 at 8:30
At the beginning can be done by spreading the tuple to a function and extracting the arguments, I can provide it in an answer if you want. At the end I think there are some unrecommended hacks that involve recursive type aliases but they are explicitly not recomended
– Titian Cernicova-Dragomir
Dec 31 '18 at 8:32
@TitianCernicova-Dragomir Yes, of course I want! Especially because I didn't quite understand the approach you wrote about :) Thanks in advance!
– Nurbol Alpysbayev
Dec 31 '18 at 8:33