TypeScript: deep partial?

You can simply create a new type, say, DeepPartial, which basically references itself (updated Jan 2022 to handle possible non-objects):

type DeepPartial<T> = T extends object ? {
    [P in keyof T]?: DeepPartial<T[P]>;
} : T;

Then, you can use it as such:

const foobar: DeepPartial<Foobar> = {
  foo: 1,
  bar: { baz: true }
};

See proof-of-concept example on TypeScript Playground.

Leave a Comment