2 Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
4 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 import {SetRequired} from 'type-fest';
16 type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
17 // type SomeRequired = {
19 // b: string; // Was already required and still is.
20 // c: boolean; // Is now required.
24 export type SetRequired<BaseType, Keys extends keyof BaseType = keyof BaseType> =
25 // Pick just the keys that are not required from the base type.
26 Pick<BaseType, Exclude<keyof BaseType, Keys>> &
27 // Pick the keys that should be required from the base type and make them required.
28 Required<Pick<BaseType, Keys>> extends
29 // If `InferredType` extends the previous, then for each key, use the inferred type key.
31 ? {[KeyType in keyof InferredType]: InferredType[KeyType]}