UNPKG

667 BTypeScriptView Raw
1import type {OptionalKeysOf} from './optional-keys-of';
2
3/**
4Creates a type that represents `true` or `false` depending on whether the given type has any optional fields.
5
6This is useful when you want to create an API whose behavior depends on the presence or absence of optional fields.
7
8@example
9```
10import type {HasOptionalKeys, OptionalKeysOf} from 'type-fest';
11
12type UpdateService<Entity extends object> = {
13 removeField: HasOptionalKeys<Entity> extends true
14 ? (field: OptionalKeysOf<Entity>) => Promise<void>
15 : never
16}
17```
18
19@category Utilities
20*/
21export type HasOptionalKeys<BaseType extends object> = OptionalKeysOf<BaseType> extends never ? false : true;