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