103

Is there a way to specify a partial type in TypeScript that also makes all child objects partials as well? For example:

interface Foobar {
  foo: number;
  bar: {
    baz: boolean;
    qux: string;
  };
}

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

This throws the following error:

TS2741: Property 'qux' is missing in type '{ baz: true; }' but required in type '{ baz: boolean; qux: string; }'.

Is there any way to make child nodes partials as well?

1

6 Answers 6

180

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.

9
  • 8
    Just beware that some folks in this thread have run into performance problems with recursive types: github.com/microsoft/TypeScript/issues/35729
    – kumar303
    Sep 3, 2020 at 2:02
  • 4
    I meant performance issues as in slow compilation times. However, since writing that comment, I've seen a lot of improvements to TS. I currently work in a code base using DeepPartial where we do not have issues.
    – kumar303
    Jun 26, 2021 at 4:02
  • 4
    In order to fix the DeepPartial<native> weird types - use this: type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T;
    – Shl
    Sep 2, 2021 at 11:48
  • 1
    @Shl Nice. Problems came up everywhere after a recent TS upgrade & this fixed it; this answer should be updated Oct 25, 2021 at 16:32
  • 1
    @Shi And I was wondering where all those strange errors(type not assignable to some other type) were coming up from. Big thanks.
    – Eggy
    Dec 18, 2021 at 19:58
39

If you're looking for a quick and easy solution, check out the type-fest package, which has many useful prebuilt TypeScript types including the PartialDeep type.

For a more technical and customizable solution, see this answer.

4
  • 3
    type-fest seems to be a more popular option: npmtrends.com/type-fest-vs-utility-types Aug 3, 2020 at 17:58
  • Thank you! This even works in a plain JS project when using jsdoc and vscode intellisense.
    – Nick Bilyk
    Feb 1, 2021 at 20:58
  • Thank you. Looking at the source code for type-fest helped me figure out how to make my DeepPartial implementation work when the passed type contains any. Jun 11, 2021 at 16:48
  • 1
    I don't think installing a whole package just for a single type is a good idea. This kinds of choices is what leads to huge redundant node_modules by javascript/ts developers.
    – John Doe
    May 10, 2022 at 21:37
9

I inspired myself on the answers on this question to create my own version of PartialDeep.

I stumbled upon some issues with built-in objects along the way; for my use case, I wouldn't expect a Date object to be missing some of its methods. It's either there, or it isn't.

Here's my version:

// Primitive types (+ Date) are themselves. Or maybe undefined.
type PartialDeep<T> = T extends string | number | bigint | boolean | null | undefined | symbol | Date
  ? T | undefined
  // Arrays, Sets and Maps and their readonly counterparts have their items made
  // deeply partial, but their own instances are left untouched
  : T extends Array<infer ArrayType>
  ? Array<PartialDeep<ArrayType>>
  : T extends ReadonlyArray<infer ArrayType>
  ? ReadonlyArray<ArrayType>
  : T extends Set<infer SetType>
  ? Set<PartialDeep<SetType>>
  : T extends ReadonlySet<infer SetType>
  ? ReadonlySet<SetType>
  : T extends Map<infer KeyType, infer ValueType>
  ? Map<PartialDeep<KeyType>, PartialDeep<ValueType>>
  : T extends ReadonlyMap<infer KeyType, infer ValueType>
  ? ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>>
  // ...and finally, all other objects.
  : {
      [K in keyof T]?: PartialDeep<T[K]>;
    };
1
  • 2
    Thanks! This approach worked for me (in shorter version), while T extends object had problem with optional nested fields.
    – Chakrit W
    Sep 20, 2022 at 9:58
7

Simply create a new type DeepPartial

DeepPartial basically references itself when its property is of object type to apply DeepPartial to that property's properties and so on

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

Usage

interface Foobar {
  foo: number;
  bar: {
    foo1: boolean;
    bar1: string;
  };
}

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

TS Playground Example

1

I had to use this version to prevent arrays having undefined elements:

type DeepPartial<T> = T extends any[]? T : T extends Record<string, any> ? {
  [P in keyof T]?: DeepPartial<T[P]>;
} : T;
0
1

I found that the examples of DeepPartial above resulted in some strange behavior for my more complicated interfaces, but they were a great place to start. Here is what I settled on for making an unopinionated DeepPartial (incidentally works for non-object types as well)

type OptionalArrayOr<T, Otherwise> = T extends T[] ? T[] | undefined : Otherwise;
type OptionalUndefinedOr<T, Otherwise> = T extends undefined ? undefined : Otherwise;
type OptionalNullOr<T, Otherwise> = T extends null ? null | undefined : Otherwise;
type OptionalStringOr<T, Otherwise> = T extends string ? T | undefined : Otherwise;
type OptionalNumberOr<T, Otherwise> = T extends number ? T | undefined : Otherwise;
type OptionalBooleanOr<T, Otherwise> = T extends boolean ? boolean | undefined : Otherwise;


type DeepPartial<T> =
    OptionalStringOr<T,
        OptionalNumberOr<T,
            OptionalBooleanOr<
                T,
                OptionalNullOr<
                    T,
                    OptionalUndefinedOr<
                        T,
                        OptionalArrayOr<
                            T,
                            T extends object ? { [Key in keyof T]?: DeepPartial<T[Key]>} : undefined
                        >
                    >
                >
            >
        >
    >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.