type IsUndefined = T & undefined extends never ? false : true; /** * UndefinedAsOptional marks all properties that can be `undefined` as optional * * @example * type Foo = { * a: string * b: number | undefined * } * type Bar = UndefinedAsOptional * * // Bar is equivalent to: * type Bar = { * a: string * b?: number * } * * const x: Foo = { a: 'foo' } // Error: Property 'b' is missing in type '{ a: string; }' but required in type 'Foo' * const y: Bar = { a: 'foo' } // OK **/ export type UndefinedAsOptional = { [K in keyof T as IsUndefined extends false ? K : never]: T[K]; } & { [K in keyof T as IsUndefined extends true ? K : never]?: T[K]; }; export {};