Sha256: 952c4a8d2338e19ef26c1c0758815b1de6c082a485f88368f5bece1e555f39d4
Contents?: true
Size: 1.08 KB
Versions: 28
Compression:
Stored size: 1.08 KB
Contents
import type {Simplify} from './simplify'; // Returns `never` if the key is optional otherwise return the key type. type RequiredFilter<Type, Key extends keyof Type> = undefined extends Type[Key] ? Type[Key] extends undefined ? Key : never : Key; // Returns `never` if the key is required otherwise return the key type. type OptionalFilter<Type, Key extends keyof Type> = undefined extends Type[Key] ? Type[Key] extends undefined ? never : Key : never; /** Enforce optional keys (by adding the `?` operator) for keys that have a union with `undefined`. @example ``` import type {EnforceOptional} from 'type-fest'; type Foo = { a: string; b?: string; c: undefined; d: number | undefined; }; type FooBar = EnforceOptional<Foo>; // => { // a: string; // b?: string; // c: undefined; // d?: number; // } ``` @internal @category Object */ export type EnforceOptional<ObjectType> = Simplify<{ [Key in keyof ObjectType as RequiredFilter<ObjectType, Key>]: ObjectType[Key] } & { [Key in keyof ObjectType as OptionalFilter<ObjectType, Key>]?: Exclude<ObjectType[Key], undefined> }>;
Version data entries
28 entries across 28 versions & 2 rubygems