preserves/implementations/javascript/src/tuple.ts

16 lines
560 B
TypeScript

// This Tuple type (and tuple() function) is a hack to induce
// TypeScript to infer tuple types rather than array types. (Source:
// https://github.com/microsoft/TypeScript/issues/27179#issuecomment-422606990)
//
// Without it, [123, 'hi', true] will often get the type (string |
// number | boolean)[] instead of [number, string, boolean].
export type Tuple<T> = T[] | [T];
export const tuple = <T, A extends Tuple<T>>(... args: A) => args;
export const Tuple = Array;
export type TupleMap<T extends Tuple<any>, V> = Tuple<V> & {
[K in keyof T]: V;
}