Skip to content

Instantly share code, notes, and snippets.

@ryangoree
Last active January 20, 2025 23:17
Show Gist options
  • Select an option

  • Save ryangoree/eec579fdf6cdb79220c7206b0232e768 to your computer and use it in GitHub Desktop.

Select an option

Save ryangoree/eec579fdf6cdb79220c7206b0232e768 to your computer and use it in GitHub Desktop.
ByIndex
/**
* Converts array or tuple `T` to an object keyed by index.
*
* Example:
* type ExampleTuple = ByIndex<[string, number]>; // { 0: string, 1: number }
* type ExampleArray = ByIndex<(string | number)[]>; // { [x: number]: string | number }
*/
type ByIndex<T extends any[]> = {
[K in Extract<keyof T, `${number}`>]: T[K];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment