Deep Partials In TypeScript

Perfect for mocking complex types for unit tests

Posted on March 21, 2022

Tags:

interface IDeepPartialArray<T> extends Array<DeepPartial<T>> {}

type DeepPartialObject<T> = {
    [Key in keyof T]?: DeepPartial<T[Key]>
}

export type DeepPartial<T> = T extends Function 
? T 
: T extends Array<infer U>
? IDeepPartialArray<U> : T extends object
? DeepPartialObject<T>
: T | undefined;

Next or Previous Post:

Or find more posts by tag:

-~{/* */}~-