UNPKG

1.61 kBTypeScriptView Raw
1import { ReverseHead, ReverseTail } from '../types';
2type UnknownFunction = (...args: any[]) => any;
3type LongestTuple<T> = T extends [infer U extends unknown[]] ? U : T extends [infer U, ...infer R extends unknown[][]] ? MostProperties<U, LongestTuple<R>> : never;
4type MostProperties<T, U> = keyof U extends keyof T ? T : U;
5type ElementAt<T, N extends keyof any> = N extends keyof T ? T[N] : unknown;
6type ElementsAt<T, N extends keyof any> = {
7 [K in keyof T]: ElementAt<T[K], N>;
8};
9type Intersect<T extends readonly unknown[]> = T extends [] ? unknown : T extends [infer H, ...infer T] ? H & Intersect<T> : T[number];
10type MergeTuples<T, L extends unknown[] = LongestTuple<T>> = {
11 [K in keyof L]: Intersect<ElementsAt<T, K> extends readonly unknown[] ? ElementsAt<T, K> : never>;
12};
13type ExtractParameters<T extends readonly UnknownFunction[]> = {
14 [K in keyof T]: Parameters<T[K]>;
15};
16export type MergeParameters<T extends readonly UnknownFunction[]> = '0' extends keyof T ? MergeTuples<MakeRestExplicit<ExtractParameters<T>>> : Parameters<T[number]>;
17type HasRest<S extends readonly unknown[]> = number extends S['length'] ? true : false;
18type HasExplicit<S extends readonly unknown[]> = '0' extends keyof S ? true : false;
19type HasCombined<S extends readonly unknown[]> = true extends HasExplicit<S> & HasRest<S> ? true : false;
20type MakeRestExplicit<T extends readonly unknown[][]> = true extends HasCombined<T> ? [
21 ...ReverseTail<T>,
22 ReverseHead<T> extends readonly unknown[] ? ReverseHead<T>[number] : never
23] : true extends HasRest<T> ? [...T] : T;
24export {};