forked from sindresorhus/type-fest
/
non-record-keys-of.d.ts
36 lines (34 loc) · 1.05 KB
/
non-record-keys-of.d.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
/**
Get the keys of a type that are not records. This is a helper type for `DeepFlattenRecord`.
@see DeepFlattenRecord
@example
'''
import type { NonRecordKeysOf } from 'type-fest';
type ExampleType = {
arrayKey: string[];
objectKey: {
nestedTwoArrayKey: string[]
nestedTwoObjectKey?: {
nestedThreeNumberKey: number;
}
nestedTwoStringKey: string;
nestedTwoFunctionKey: () => void;
}
stringKey: string;
numberKey: number;
functionKey: () => void;
}
type ExampleTypeNonRecordKeys = NonRecordKeysOf<ExampleType>;
//=> 'arrayKey' | 'objectKey' | 'stringKey' | 'numberKey' | 'functionKey'
'''
*/
export type NonRecordKeysOf<BaseType extends Record<string | number | symbol, unknown>> = {
[Key in keyof BaseType]: BaseType[Key] extends unknown[]
// Include arrays
? Key
: BaseType[Key] extends Function
// Include functions
? Key
// Check if the value is a Record, return never if it is, otherwise return the key. (Exclude Records)
: NonNullable<BaseType[Key]> extends Record<string | number | symbol, unknown> ? never : Key
}[keyof BaseType];