DeepPartial

Make all properties in an object optional recursively.

#types #typescript #utility-type
export type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object
? DeepPartial<T[K]>
: T[K];
};
// Usage
type User = {
name: string;
address: {
city: string;
zip: number;
};
};
type PartialUser = DeepPartial<User>;
// {
// name?: string;
// address?: {
// city?: string;
// zip?: number;
// };
// }

Share this snippet

Comments