1 import {Except} from './except';
4 Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
6 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.
10 import {SetOptional} from 'type-fest';
18 type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
19 // type SomeOptional = {
21 // b?: string; // Was already optional and still is.
22 // c?: boolean; // Is now optional.
26 export type SetOptional<BaseType, Keys extends keyof BaseType = keyof BaseType> =
27 // Pick just the keys that are not optional from the base type.
28 Except<BaseType, Keys> &
29 // Pick the keys that should be optional from the base type and make them optional.
30 Partial<Pick<BaseType, Keys>> extends
31 // If `InferredType` extends the previous, then for each key, use the inferred type key.
33 ? {[KeyType in keyof InferredType]: InferredType[KeyType]}