Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
914 bytes
|
Line | |
---|
1 | import {Except} from './except';
|
---|
2 | import {Simplify} from './simplify';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
|
---|
6 |
|
---|
7 | Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are required.
|
---|
8 |
|
---|
9 | @example
|
---|
10 | ```
|
---|
11 | import {SetRequired} from 'type-fest';
|
---|
12 |
|
---|
13 | type Foo = {
|
---|
14 | a?: number;
|
---|
15 | b: string;
|
---|
16 | c?: boolean;
|
---|
17 | }
|
---|
18 |
|
---|
19 | type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
|
---|
20 | // type SomeRequired = {
|
---|
21 | // a?: number;
|
---|
22 | // b: string; // Was already required and still is.
|
---|
23 | // c: boolean; // Is now required.
|
---|
24 | // }
|
---|
25 | ```
|
---|
26 | */
|
---|
27 | export type SetRequired<BaseType, Keys extends keyof BaseType> =
|
---|
28 | Simplify<
|
---|
29 | // Pick just the keys that are optional from the base type.
|
---|
30 | Except<BaseType, Keys> &
|
---|
31 | // Pick the keys that should be required from the base type and make them required.
|
---|
32 | Required<Pick<BaseType, Keys>>
|
---|
33 | >;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.