Type alias DeepNotRequired<T>

DeepNotRequired<T>: T extends UnknownFunction
    ? T
    : {
        [K in Keys<T>]?: IfExtends<T[K], unknown, DeepNotRequired<T[K]>, T[K]>
    }

Why not call it DeepOptional? Optional<T> in this library Optional represents a type T that can be either T or null. So creating DeepOptional type would entail adding null to every property, which is not the intention here.

DeepNotRequired<T> turns all required keys in a given object (nested) to non required one. non required as in: marked with ? operator

Type Parameters

  • T

Example

type Actual = {
a: () => 1;
x: string;
s: {
q: Nullable;
s: {
i: {
x: {
o: Maybe<Primitive>;
n: Falsy;
};
e: 'foo';
};
};
};
};

type Expected = {
a?: () => 1;
x?: string;
s?: {
q?: Nullable;
s?: {
i?: {
x?: {
o?: Maybe<Primitive>;
n?: Falsy;
};
e?: 'foo';
};
};
};
};
type T = DeepNotRequired<Actual>; // Result: Expected