-
-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathrequired-keys-of.d.ts
More file actions
38 lines (29 loc) · 1.18 KB
/
required-keys-of.d.ts
File metadata and controls
38 lines (29 loc) · 1.18 KB
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
import type {OptionalKeysOf} from './optional-keys-of.d.ts';
/**
Extract all required keys from the given type.
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
@example
```
import type {RequiredKeysOf} from 'type-fest';
declare function createValidation<
Entity extends object,
Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
>(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
type User = {
name: string;
surname: string;
luckyNumber?: number;
};
const validator1 = createValidation<User>('name', value => value.length < 25);
const validator2 = createValidation<User>('surname', value => value.length < 25);
// @ts-expect-error
const validator3 = createValidation<User>('luckyNumber', value => value > 0);
// Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
```
@category Utilities
*/
export type RequiredKeysOf<Type extends object> =
Type extends unknown // For distributing `Type`
? Exclude<keyof Type, OptionalKeysOf<Type>>
: never; // Should never happen
export {};