Effect CommunityEC
Effect Community4y ago
10 replies
ts

undefined vs optional

Is there a way to make this utility type work for optional keys?
type UndefinedToNull<T> = T extends undefined
    ? null
    : T extends Date
    ? T
    : {
          [K in keyof T]: UndefinedToNull<T[K]>;
      };

type A = {
    foo?: string;
    bar: string | undefined;
};

// type B = {
//     foo?: string | null | undefined;
//     bar: string | null;
// }
type B = UndefinedToNull<A>;


I would want the optional key to be string | null as well
Was this page helpful?