Skip to content

Instantly share code, notes, and snippets.

@ScriptRaccoon
Created July 7, 2023 11:52
Show Gist options
  • Save ScriptRaccoon/32c354ffd8acac173affe94e24116e1e to your computer and use it in GitHub Desktop.
Save ScriptRaccoon/32c354ffd8acac173affe94e24116e1e to your computer and use it in GitHub Desktop.
Tuple types in TypeScript with fixed length
type Tuple<Length extends number, T, Accumulator extends T[] = []> =
Accumulator["length"] extends Length ?
Accumulator :
Tuple<Length,T,[...Accumulator,T]>;
const example: Tuple<3,string> = ["hi","mom","!"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment