frontend/.pnpm-store/v3/files/c6/773be5919d4fe2d80495a4d62f0cf12439a6decbd6307e32398293b28bc74dd344e9861fc49235060686e1546596bd9cc92b2e233b54d5eaa0b902aaa17621

40 lines
1.1 KiB
Plaintext

/**
Create a type that makes the given keys non-nullable, where the remaining keys are kept as is.
If no keys are given, all keys will be made non-nullable.
Use-case: You want to define a single model where the only thing that changes is whether or not some or all of the keys are non-nullable.
@example
```
import type {SetNonNullable} from 'type-fest';
type Foo = {
a: number | null;
b: string | undefined;
c?: boolean | null;
}
type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
// type SomeNonNullable = {
// a: number | null;
// b: string; // Can no longer be undefined.
// c?: boolean; // Can no longer be null, but is still optional.
// }
type AllNonNullable = SetNonNullable<Foo>;
// type AllNonNullable = {
// a: number; // Can no longer be null.
// b: string; // Can no longer be undefined.
// c?: boolean; // Can no longer be null, but is still optional.
// }
```
@category Object
*/
export type SetNonNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = {
[Key in keyof BaseType]: Key extends Keys
? NonNullable<BaseType[Key]>
: BaseType[Key];
};