6

I was looking through this question here:

TypeScript: deep partial?

The top answer has DeepPartial type defined as follows:

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

I tested and this type works as expected, but I can't fully understand how this works internally.

let's take example interface from the same question:

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

when P=bar, it's easy to understand that recursively it will go into

DeepPartial<{
    baz: boolean;
    qux: string;
  }>

and make "baz" and "qux" keys optional.

But what I don't get is how does the recursion work for primitive types? Like for foo key, how is DeepPartial<number> same as number? When T=number,

[P in keyof number]?: DeepPartial<number[P]>;

doesn't make sense to me.

In my mind, the implementation of DeepPartial should be something like:

type DeepPartial<T> = {
    [P in keyof T]?: isPrimitive(T[P]) ? T[P] : DeepPartial<T[P]>;
};

But original implementation works also, and I don't get how.

I hope I explained my question well enough. Here is example playground:

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHt0CM5WQbwChlkZMAuZEAVwFstoBuY5HKSokknAL0q0wAbCHBDMuyAI7UAHpQDOYKKADm45AF9mGwoTABPAA4oAIhAiGACrjDA4ggDwAVAHzIAvARYkA2peSgyADWEProMMhOALoA-JRmFtZQtvbOflEu2syECOggiqSYbPHmVjZ2jhjYuG6enIXolACMADQsxQSscHzIStQoOlq6uflgvRCKAJLgJYnlqTT00LXIAKzMQA

Edit: I investigated further and found out that

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

type DeepPartialTest<T> = {
        [P in keyof number]?: any;
    };

now DeepPartial<number> will return number type, but DeepPartialTest<number> will not. Which makes this even weirder for me.

Edit 2 add screenshots:

This works as expected:

enter image description here

This doesn't:

enter image description here

When in reality both should be the same??

6
  • DeepPartial just makes the keys optional. number is a primitie and doesn't technically have keys, so DeepPartial<number> is a no-op
    – VLAZ
    Commented Jan 13, 2021 at 15:08
  • but shouldn't DeepPartial<number> return empty object ({}) type in that case and not "number" type?
    – Mister M
    Commented Jan 13, 2021 at 15:11
  • No, it shouldn't. A primitive technically doesn't have methods, it's just converted to an object when you use any methods on it. You cannot claim to have a number (e.g., 42) but you somehow cannot do (42).toString() because you cannot remove toString() from primitives. They are immutable.
    – VLAZ
    Commented Jan 13, 2021 at 15:13
  • Still don't get how DeepPartial<number> returns number type. So if I define DeepPartial<T> type as object with no keys (since keyof number doesn't have keys), I get just T as a result instead of object with no keys? Can't grasp how the logic works
    – Mister M
    Commented Jan 13, 2021 at 15:18
  • number is a primitive, not an object. Primitives are immutable, so it doesn't make sense to have different properties on 5 and 42.
    – VLAZ
    Commented Jan 13, 2021 at 15:20

2 Answers 2

4

I think the question is "why do mapped types acting on primitives produce those same primitives and why does it only seem to happen sometimes" and not about DeepPartial per se, right?


Mapped types come in two "flavors". There are normal mapped types which just iterate over some arbitrary union of property key literals (e.g., {[P in K]: ...}), and homomorphic mapped types which iterate specifically over the keys of another type (e.g., {[P in keyof T]: ...}), and try to preserve the structure of that type by, say, copying modifiers like readonly and ? from the input type to the output type (unless you change the modifiers with your own readonly or ? and + or -). Example:

interface Obj {
  a?: number,
  b: string,
  readonly c: boolean
}

type Homomorphic<T> = {
  [K in keyof T]: Array<T[K]>
}
type ObjHom = Homomorphic<Obj>
/* type ObjHom = {
    a?: (number | undefined)[] | undefined;
    b: string[];
    readonly c: boolean[];
} */

type ObjKeys = keyof Obj;

type ObjNonHom = {
  [K in ObjKeys]: Array<Obj[K]>
}
/* type ObjNonHom = {
    a: (number | undefined)[];
    b: string[];
    c: boolean[];
} */

In the above code, ObjHom and ObjNonHom are similar, but ObjHom's a property is optional and its c property is readonly (just like in Obj); while ObjNonHom's properties are all required and mutable.


Being able to determine when a particular mapped type is homomorphic or not can be tricky, since there are lots of heuristics involved. Generally speaking, a mapped type featuring in keyof T where T is some type depending on an unresolved generic parameter will be homomorphic. Generally speaking, a mapped type featuring in X where X is some type without keyof directly in it will be non-homomorphic. When a mapped type feature in keyof T where T is some non-generic type, things are iffy. For primitives like number this tends to be non-homomorphic, but for object types it tends to be homomorphic. I can probably go through GitHub and find PRs that implement each heuristic, but I don't know that it's worth anyone's time for that.


The particular behavior with primitives was introduced introduced in microsoft/TypeScript#12447. In the description, it says that:

A mapped type of the form { [P in keyof T]: X }, where T is some type parameter, is said to be [a homomorphic] mapped type because it produces a type with the same shape as T. [...] [When] a primitive type is substituted for T in an isomorphic mapped type, we simply produce that primitive type. For example, when { [P in keyof T]: X } is instantiated with A | undefined for T, we produce { [P in keyof A]: X } | undefined.


So the basic answer to the question is: when a mapped type is homomorphic and acts on a primitive type, the same primitive type comes out. The original DeepPartial is homomorphic, so number becomes number. Your modified version with in keyof number directly is non-homomorphic, so number becomes a type with each apparent property of number mapped to an optional property.

Playground link to code

0
2

This is actually an internal mechanism in TypeScript for type parameters which states that:

when a primitive type is substituted for T in an isomorphic mapped type, we simply produce that primitive type.

You can read more about it in this pull request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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