Seeking a solution that can achieve the following:
type MakeOptional<T, U> = /* ... */;
interface A {
foo: string;
bar: number;
baz: Date;
}
type AWithOptionalFoo = MakeOptional<A, 'foo'>;
// desired output: { foo?: string | undefined; bar: number; baz: number; }
type AWithOptionalBarAndBaz = MakeOptional<A, 'foo' | 'baz'>;
// desired output: { foo?: string | undefined; bar: number; baz?: Date | undefined; }
This is my attempt so far...
type MakeOptional<T, U> = { [P in keyof T]: P extends U ? T[P] | undefined : T[P] }
...however, the properties are marked as T | undefined
instead of having them appear as (optional)? T | undefined
.
Any suggestions to improve this?