I want to map all optional fields of an object to required fields with undefined as option.
From
{ isOptional?: string, required: number }
To
{ isOptional: string| undefined, required: number}
I can’t find the reason why this won’t work. Playground
type IsOptional<T extends {}, K extends keyof T> =
{ [P in keyof T]?: T[P] } extends Pick<T, K> ? true : false;
type OptionalInfo<T extends {}> = { [P in keyof T]-?: IsOptional<T, P> };
type OptionalToUndefined<T extends {}> = {
[P in keyof Required<T>]: OptionalInfo<T>[P] extends true
? T[P] | undefined
: T[P];
};
type test1 = OptionalInfo<{ isOptional?: string; required: number }>;
// {isOptional: true, required: false}
type test2 = OptionalToUndefined<{ isOptional?: string, required: number }>;
// {isOptional: string, required: number}
3