This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
192 lines (178 loc) · 4.48 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @since 0.3.0
*/
/**
* @since 0.3.0
*/
export type Compact<A> = { [K in keyof A]: A[K] }
/**
* Returns the string literal 'T' if `A` and `B` are equal types, 'F' otherwise
*
* @example
* import { Equals } from 'typelevel-ts'
*
* export type Result1 = Equals<string, string> // "T"
* export type Result2 = Equals<string, number> // "F"
*
* @since 0.3.0
*/
export type Equals<A, B> = (<C>() => C extends Compact<A> ? 'T' : 'F') extends <C>() => C extends Compact<B> ? 'T' : 'F'
? 'T'
: 'F'
/**
* @example
* import { Overwrite } from 'typelevel-ts'
*
* export type Result = Overwrite<{ a: string; b: number }, { b: boolean }> // { a: string; b: boolean }
*
* @since 0.3.0
*/
export type Overwrite<A extends object, B extends object> = Compact<Omit<A, keyof B> & B>
/**
* @example
* import { Diff } from 'typelevel-ts'
*
* export type Result = Diff<{ a: string; b: number }, 'b'> // { a: string; b?: number }
*
* @since 0.3.0
*/
export type Diff<A extends object, OK extends keyof A> = Compact<
{ [K in Exclude<keyof A, OK>]: A[K] } & { [K in OK]?: A[K] }
>
/**
* Picks only the keys of a certain type
*
* @example
* import { KeysOfType } from 'typelevel-ts'
*
* export type Result = KeysOfType<{a: string, b: string | boolean, c: boolean, d: string}, string> // "a" | "d"
*
* @since 0.3.0
*/
export type KeysOfType<A extends object, B> = { [K in keyof A]-?: A[K] extends B ? K : never }[keyof A]
/**
* Encodes the constraint that a given object `A` does not contain specific keys `K`
*
* @example
* import { RowLacks } from 'typelevel-ts'
*
* // function f(x: RowLacks<{ a: string; b: number }, 'a' | 'b'>): void {}
* // $ExpectError
* // f({ a: 'a', b: 1 })
* function g(x: RowLacks<{ a: string; b: number }, 'c'>): void {}
* g({ a: 'a', b: 1 }) // ok
*
* @since 0.3.0
*/
export type RowLacks<A extends object, K extends string | number | symbol> = A & Record<Extract<keyof A, K>, never>
/**
* @example
* import { Exact } from 'typelevel-ts'
*
* function f<T extends Exact<{ a: string }, T>>(a: T): void {}
* f({ a: 'a' })
* // $ExpectError
* // f({ a: 'a', b: 1 })
*
* @since 0.3.0
*/
export type Exact<A extends object, B extends A> = A & Record<Exclude<keyof B, keyof A>, never>
/**
* @example
* import { AnyTuple } from 'typelevel-ts'
*
* function f<T extends AnyTuple>(x: T): T {
* return x
* }
* const x: [number] = [1]
* const y: [number, string] = [1, 'a']
* const z: [number, string, boolean] = [1, 'a', true]
* f(x)
* f(y)
* f(z)
* // $ExpectError
* // f([1, 2, 3])
*
* @since 0.3.0
*/
export type AnyTuple = Array<any> & { '0': any }
/**
* @internal
* @since 0.3.0
*/
export interface DeepReadonlyArray<A> extends ReadonlyArray<DeepReadonly<A>> {}
/**
* @internal
* @since 0.3.0
*/
export type DeepReadonlyObject<A> = { readonly [K in keyof A]: DeepReadonly<A[K]> }
/**
* @example
* import { DeepReadonly } from 'typelevel-ts'
*
* interface Foo {
* bar: {
* baz: string
* quux: Array<{ barbaz: number }>
* }
* }
*
* type ReadonlyFoo = DeepReadonly<Foo>
* export declare const x: ReadonlyFoo
* // $ExpectError
* // x.bar.quux[1].barbaz = 1
*
* @since 0.3.0
*/
export type DeepReadonly<A> = A extends Array<infer B> ? DeepReadonlyArray<B> : DeepReadonlyObject<A>
/**
* Extracts the type of a member of a tagged union
*
* @example
* import { TaggedUnionMember } from 'typelevel-ts'
*
* type A = { tag: 'A'; a: string }
* type B = { tag: 'B'; b: number }
* type C = A | B
* export type Result = TaggedUnionMember<C, 'tag', 'A'> // A
*
* @since 0.3.0
*/
export type TaggedUnionMember<A extends object, Tag extends keyof A, Value extends A[Tag]> = Extract<
A,
Record<Tag, Value>
>
/**
* Extracts required keys as a literal type union
*
* @example
* import { RequiredKeys } from 'typelevel-ts'
*
* type A = { a: string; b: number; x?: string; y?: number }
* export type Result = RequiredKeys<A> // "a" | "b"
*
* @since 0.3.0
*/
export type RequiredKeys<T> = { [K in keyof T]: {} extends Pick<T, K> ? never : K } extends { [_ in keyof T]: infer U }
? {} extends U
? never
: U
: never
/**
* Extracts optional keys as a literal type union
*
* @example
* import { OptionalKeys } from 'typelevel-ts'
*
* type A = { a: string; b: number; x?: string; y?: number }
* export type Result = OptionalKeys<A> // "x" | "y"
*
* @since 0.3.0
*/
export type OptionalKeys<T> = { [K in keyof T]: T extends Record<K, T[K]> ? never : K } extends {
[_ in keyof T]: infer U
}
? {} extends U
? never
: U
: never