1
- import { Diff } from './strings' ;
2
1
import { False , True } from './conditionals' ;
3
- import { AnyFunc } from './functions' ;
4
2
5
3
// -------
6
4
// Helpers
@@ -16,7 +14,7 @@ export type PlainObject = Record<string, any>;
16
14
* @returns An object formed by the key, value pairs of T
17
15
*/
18
16
export type ObjectType < T > = {
19
- [ k in Keys < T > ] : T [ k ] ;
17
+ [ k in keyof T ] : T [ k ] ;
20
18
} ;
21
19
/**
22
20
* Takes two objects and returns their intersection.
@@ -35,7 +33,7 @@ export type CombineObjects<T extends object, U extends object> = ObjectType<T &
35
33
* @param K Key to query object for value
36
34
* @returns `T[K]` if the key exists, `never` otherwise
37
35
*/
38
- export type GetKey < T , K extends keyof any > = K extends Keys < T > ? T [ K ] : never ;
36
+ export type GetKey < T , K extends keyof any > = K extends keyof T ? T [ K ] : never ;
39
37
40
38
// ----
41
39
// Keys
@@ -56,51 +54,45 @@ export type ObjectKeys = keyof any;
56
54
* @param T type from which to get keys
57
55
* @returns keys of `T` that extend `string`
58
56
*/
59
- export type StringKeys < T > = Exclude < Keys < T > , number | symbol > ;
60
- /**
61
- * No different than `keyof`, but can look a bit nicer when nesting many types deep.
62
- * @param T type from which to get keys
63
- * @returns keys of `T` that extend `string | number | symbol`
64
- */
65
- export type Keys < T > = keyof T ;
57
+ export type StringKeys < T > = Exclude < keyof T , number | symbol > ;
66
58
/**
67
59
* When an object has optional or readonly keys, that information is contained within the key.
68
60
* When using optional/readonly keys in another object, they will retain optional/readonly status.
69
61
* `PureKeys` will remove the optional/readonly status modifiers from keys.
70
62
* @param T type from which to get keys
71
63
* @returns keys of `T` without status modifiers (readonly/optional)
72
64
*/
73
- export type PureKeys < T > = Record < Keys < T > , Keys < T > > [ Keys < T > ] ;
65
+ export type PureKeys < T > = Record < keyof T , keyof T > [ keyof T ] ;
74
66
/**
75
67
* Gets all of the keys that are shared between two objects.
76
68
* @param T first type from which keys will be pulled
77
69
* @param U second type from which keys will be pulled
78
70
* @returns the keys that both `T` and `U` have in common.
79
71
*/
80
- export type SharedKeys < T , U > = Keys < T > & Keys < U > ;
72
+ export type SharedKeys < T , U > = keyof T & keyof U ;
81
73
/**
82
74
* Gets all keys between two objects.
83
75
* @param T first type from which keys will be pulled
84
76
* @param U second type from which keys will be pulled
85
77
* @returns the keys of `T` in addition to the keys of `U`
86
78
*/
87
- export type AllKeys < T , U > = Keys < T > | Keys < U > ;
79
+ export type AllKeys < T , U > = keyof T | keyof U ;
88
80
/**
89
81
* Gets all of the keys that are different between two objects.
90
- * This is a set difference between `Keys<T> ` and `Keys<U> `.
82
+ * This is a set difference between `keyof T ` and `keyof U `.
91
83
* Note that calling this with arguments reversed will have different results.
92
84
* @param T first type from which keys will be pulled
93
85
* @param U second type from which keys will be pulled
94
86
* @returns keys of `T` minus the keys of `U`
95
87
*/
96
- export type DiffKeys < T , U > = Diff < Keys < T > , Keys < U > > ;
88
+ export type DiffKeys < T , U > = Exclude < keyof T , keyof U > ;
97
89
/**
98
90
* Returns `True` if a key, `K`, is present in a type, `T`, else `False`.
99
91
* @param T type to check for existence of key `K`.
100
92
* @param K key to query `T` for
101
93
* @returns `True` if `K` is a key of `T`. Else `False`.
102
94
*/
103
- export type HasKey < T , K extends keyof any > = K extends Keys < T > ? True : False ;
95
+ export type HasKey < T , K extends keyof any > = K extends keyof T ? True : False ;
104
96
105
97
/**
106
98
* @param T the union to get the keys of
@@ -109,7 +101,7 @@ export type HasKey<T, K extends keyof any> = K extends Keys<T> ? True : False;
109
101
export type UnionKeys < T >
110
102
// Using a conditional here, so that it distributes over members of the union
111
103
// See https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types
112
- = T extends any ? keyof T : never ;
104
+ = T extends unknown ? keyof T : never ;
113
105
114
106
// -------------
115
107
// Manipulations
@@ -119,15 +111,15 @@ export type UnionKeys<T>
119
111
* @param T the object whose property values will be unionized
120
112
* @returns a union of the right-side values of `T`
121
113
*/
122
- export type UnionizeProperties < T extends object > = T [ Keys < T > ] ;
114
+ export type UnionizeProperties < T extends object > = T [ keyof T ] ;
123
115
/**
124
116
* Gives back an object with listed keys removed.
125
117
* This is the opposite of `Pick`.
126
118
* @param T the object whose properties will be removed
127
119
* @param K the union of keys to remove from `T`
128
120
* @returns `T` with the keys `K` removed
129
121
*/
130
- export type Omit < T extends object , K extends Keys < T > > = Pick < T , Diff < Keys < T > , K > > ;
122
+ export type Omit < T extends object , K extends keyof T > = Pick < T , Exclude < keyof T , K > > ;
131
123
/**
132
124
* Returns only the shared properties between two objects.
133
125
* All shared properties must be the same type.
@@ -161,7 +153,7 @@ export type Merge<T extends object, U extends object> = Overwrite<T, U> & U;
161
153
* @returns a record where each key of the record is now the `Key` property of the inner object
162
154
*/
163
155
export type TaggedObject < T extends Record < keyof any , object > , Key extends keyof any > = {
164
- [ K in Keys < T > ] : T [ K ] & Record < Key , K > ;
156
+ [ K in keyof T ] : T [ K ] & Record < Key , K > ;
165
157
} ;
166
158
167
159
// ---------
@@ -174,9 +166,9 @@ export type TaggedObject<T extends Record<keyof any, object>, Key extends keyof
174
166
* @returns `Partial<T>` recursively through all properties of `T`
175
167
*/
176
168
export type DeepPartial < T > = Partial < {
177
- [ k in Keys < T > ] :
178
- T [ k ] extends any [ ] ? Array < DeepPartial < T [ k ] [ number ] > > :
179
- T [ k ] extends AnyFunc ? T [ k ] :
169
+ [ k in keyof T ] :
170
+ T [ k ] extends unknown [ ] ? Array < DeepPartial < T [ k ] [ number ] > > :
171
+ T [ k ] extends Function ? T [ k ] :
180
172
T [ k ] extends object ? DeepPartial < T [ k ] > :
181
173
T [ k ] ;
182
174
} > ;
@@ -186,15 +178,15 @@ export type DeepPartial<T> = Partial<{
186
178
* @returns `T` with all fields marked required
187
179
*/
188
180
export type AllRequired < T extends object > = {
189
- [ K in Keys < T > ] -?: NonNullable < T [ K ] >
181
+ [ K in keyof T ] -?: NonNullable < T [ K ] >
190
182
} ;
191
183
/**
192
184
* Mark specific keys, `K`, of `T` as required.
193
185
* @param T object whose keys will be marked required
194
186
* @param K keys of `T` that will be marked required
195
187
* @returns `T` with keys, `K`, marked as required
196
188
*/
197
- export type Required < T extends object , K extends Keys < T > > = CombineObjects <
189
+ export type Required < T extends object , K extends keyof T > = CombineObjects <
198
190
{ [ k in K ] -?: NonNullable < T [ k ] > } ,
199
191
Omit < T , K >
200
192
> ;
@@ -204,7 +196,7 @@ export type Required<T extends object, K extends Keys<T>> = CombineObjects<
204
196
* @param K keys of `T` that will be marked optional
205
197
* @returns `T` with keys, `K`, marked as optional
206
198
*/
207
- export type Optional < T extends object , K extends Keys < T > > = CombineObjects <
199
+ export type Optional < T extends object , K extends keyof T > = CombineObjects <
208
200
{ [ k in K ] ?: T [ k ] | undefined } ,
209
201
Omit < T , K >
210
202
> ;
@@ -214,9 +206,9 @@ export type Optional<T extends object, K extends Keys<T>> = CombineObjects<
214
206
* @returns `T` with all keys recursively marked as readonly
215
207
*/
216
208
export type DeepReadonly < T > = Readonly < {
217
- [ k in Keys < T > ] :
218
- T [ k ] extends any [ ] ? ReadonlyArray < DeepReadonly < T [ k ] [ number ] > > :
219
- T [ k ] extends AnyFunc ? T [ k ] :
209
+ [ k in keyof T ] :
210
+ T [ k ] extends unknown [ ] ? ReadonlyArray < DeepReadonly < T [ k ] [ number ] > > :
211
+ T [ k ] extends Function ? T [ k ] :
220
212
T [ k ] extends object ? DeepReadonly < T [ k ] > :
221
213
T [ k ] ;
222
214
} > ;
@@ -261,6 +253,6 @@ export type StrictUnion<T> = _StrictUnionHelper<T, T>;
261
253
// to refer to each individual member of the union
262
254
/** no-doc */
263
255
export type _StrictUnionHelper < UnionMember , Union > =
264
- UnionMember extends any ?
256
+ UnionMember extends unknown ?
265
257
UnionMember & Partial < Record < Exclude < UnionKeys < Union > , keyof UnionMember > , never > >
266
258
: never ;
0 commit comments