Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Created November 15, 2019 14:19
Show Gist options
  • Save swissmanu/9038ec6c15f67bc8b543df8dc498f294 to your computer and use it in GitHub Desktop.
Save swissmanu/9038ec6c15f67bc8b543df8dc498f294 to your computer and use it in GitHub Desktop.
export type Tuple2<A, B> = [A, B];
export type Tuple3<A, B, C> = [A, B, C];
export type Tuple4<A, B, C, D> = [A, B, C, D];
export type Tuple5<A, B, C, D, E> = [A, B, C, D, E];
export type Tuple6<A, B, C, D, E, F> = [A, B, C, D, E, F];
export type Tuple7<A, B, C, D, E, F, G> = [A, B, C, D, E, F, G];
export type Tuple8<A, B, C, D, E, F, G, H> = [A, B, C, D, E, F, G, H];
export type Tuple9<A, B, C, D, E, F, G, H, I> = [A, B, C, D, E, F, G, H, I];
export type Tuple10<A, B, C, D, E, F, G, H, I, J> = [A, B, C, D, E, F, G, H, I, J];
export function tuple<A, B>(_1: A, _2: B): Tuple2<A, B>;
export function tuple<A, B, C>(_1: A, _2: B, _3: C): Tuple3<A, B, C>;
export function tuple<A, B, C, D>(_1: A, _2: B, _3: C, _4: D): Tuple4<A, B, C, D>;
export function tuple<A, B, C, D, E>(_1: A, _2: B, _3: C, _4: D, _5: E): Tuple5<A, B, C, D, E>;
export function tuple<A, B, C, D, E, F>(_1: A, _2: B, _3: C, _4: D, _5: E, _6: F): Tuple6<A, B, C, D, E, F>;
export function tuple<A, B, C, D, E, F, G>(
_1: A,
_2: B,
_3: C,
_4: D,
_5: E,
_6: F,
_7: G
): Tuple7<A, B, C, D, E, F, G>;
export function tuple<A, B, C, D, E, F, G, H>(
_1: A,
_2: B,
_3: C,
_4: D,
_5: E,
_6: F,
_7: G,
_8: H
): Tuple8<A, B, C, D, E, F, G, H>;
export function tuple<A, B, C, D, E, F, G, H, I>(
_1: A,
_2: B,
_3: C,
_4: D,
_5: E,
_6: F,
_7: G,
_8: H,
_9: I
): Tuple9<A, B, C, D, E, F, G, H, I>;
export function tuple<A, B, C, D, E, F, G, H, I, J>(
_1: A,
_2: B,
_3: C,
_4: D,
_5: E,
_6: F,
_7: G,
_8: H,
_9: I,
_10: J
): Tuple10<A, B, C, D, E, F, G, H, I, J>;
/**
* Creates a `Tuple` with typed elements. Use array destructuring to access specific elements.
*
* ### Example
*
* ```
const [aNumber, , aBoolean] = tuple(1, 'test', true);
```
*
* ### Reference
* - https://en.wikipedia.org/wiki/Tuple
*/
export function tuple<A, B, C, D, E, F, G, H, I, J>(
_1: A,
_2: B,
_3?: C,
_4?: D,
_5?: E,
_6?: F,
_7?: G,
_8?: H,
_9?: I,
_10?: J
) {
if (arguments.length === 2) {
return [_1, _2];
} else if (arguments.length === 3) {
return [_1, _2, _3];
} else if (arguments.length === 4) {
return [_1, _2, _3, _4];
} else if (arguments.length === 5) {
return [_1, _2, _3, _4, _5];
} else if (arguments.length === 6) {
return [_1, _2, _3, _4, _5, _6];
} else if (arguments.length === 7) {
return [_1, _2, _3, _4, _5, _6, _7];
} else if (arguments.length === 8) {
return [_1, _2, _3, _4, _5, _6, _7, _8];
} else if (arguments.length === 9) {
return [_1, _2, _3, _4, _5, _6, _7, _8, _9];
} else if (arguments.length === 10) {
return [_1, _2, _3, _4, _5, _6, _7, _8, _9, _10];
} else {
throw new Error(`Unsupported Tuple length of ${arguments.length} elements`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment