forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
has-required-keys.d.ts
59 lines (48 loc) · 1.16 KB
/
has-required-keys.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {RequiredKeysOf} from './required-keys-of';
/**
Creates a type that represents `true` or `false` depending on whether the given type has any required fields.
This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.
@example
```
import type {HasRequiredKeys} from 'type-fest';
type GeneratorOptions<Template extends object> = {
prop1: number;
prop2: string;
} & (HasRequiredKeys<Template> extends true
? {template: Template}
: {template?: Template});
interface Template1 {
optionalSubParam?: string;
}
interface Template2 {
requiredSubParam: string;
}
type Options1 = GeneratorOptions<Template1>;
type Options2 = GeneratorOptions<Template2>;
const optA: Options1 = {
prop1: 0,
prop2: 'hi'
};
const optB: Options1 = {
prop1: 0,
prop2: 'hi',
template: {}
};
const optC: Options1 = {
prop1: 0,
prop2: 'hi',
template: {
optionalSubParam: 'optional value'
}
};
const optD: Options2 = {
prop1: 0,
prop2: 'hi',
template: {
requiredSubParam: 'required value'
}
};
```
@category Utilities
*/
export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;