Created
March 15, 2021 04:03
-
-
Save mscolnick/ceb6e7aa244fbcf7b1bf4e84658e5b9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variadic tuple elements | |
type Foo<T extends unknown[]> = [string, ...T, number]; | |
type T1 = Foo<[boolean]>; // [string, boolean, number] | |
type T2 = Foo<[number, number]>; // [string, number, number, number] | |
type T3 = Foo<[]>; // [string, number] | |
// Strongly typed tuple concatenation | |
function concat<T extends unknown[], U extends unknown[]>(t: [...T], u: [...U]): [...T, ...U] { | |
return [...t, ...u]; | |
} | |
const ns = [0, 1, 2, 3]; // number[] | |
const t1 = concat([1, 2], ['hello']); // [number, number, string] | |
const t2 = concat([true], t1); // [boolean, number, number, string] | |
const t3 = concat([true], ns); // [boolean, ...number[]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment