UNPKG

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