Skip to content

Instantly share code, notes, and snippets.

@mscolnick
Created March 15, 2021 04:03
Show Gist options
  • Save mscolnick/ceb6e7aa244fbcf7b1bf4e84658e5b9c to your computer and use it in GitHub Desktop.
Save mscolnick/ceb6e7aa244fbcf7b1bf4e84658e5b9c to your computer and use it in GitHub Desktop.
// 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