This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hkts.ts
51 lines (44 loc) · 2.01 KB
/
hkts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/***************************************************************************************************
* @section Hole Types
* @description Marks a type hole to be filled by the substitution ($) type
**************************************************************************************************/
declare const index: unique symbol;
export interface _<N extends number = 0> {
[index]: N;
}
export type _0 = _<0>;
export type _1 = _<1>;
export type _2 = _<2>;
export type _3 = _<3>;
export type _4 = _<4>;
export type _5 = _<5>;
export type _6 = _<6>;
export type _7 = _<7>;
export type _8 = _<8>;
export type _9 = _<9>;
/***************************************************************************************************
* @section Fix Type
* @description Fixes a type so it is not replaced by the substitution ($) type
**************************************************************************************************/
declare const Fix: unique symbol;
export interface Fix<T> {
[Fix]: T;
}
/***************************************************************************************************
* @section Substitution Type
* @description Replaces any type holes in a type with the supplied parameters
* @example
* type FunctorFn<T> = <A, B>(fab: (a: A) => B, ta: $<T, [A]>) => $<T, [B]>;
* type ArrayInstance = FunctorFn<Array<_>>;
* // ArrayInstance = <A, B>(fab: (a: A) => B, ta: A[]): B[]
* type RecordInstance = FunctorFn<{ value: _ }>;
* // RecordInstance = <A, B>(fab: (a: A) => B, ta: { value: A }): { value: B }
**************************************************************************************************/
export type $<T, S extends any[]> = T extends Fix<infer U> ? U
: T extends _<infer N> ? S[N]
: T extends any[] ? { [K in keyof T]: $<T[K], S> }
: T extends Promise<infer I> ? Promise<$<I, S>>
: T extends (...x: infer I) => infer O ? (...x: $<I, S>) => $<O, S>
: T extends object ? { [K in keyof T]: $<T[K], S> }
: T extends undefined | null | boolean | string | number ? T
: T;