9. Generics and Conditional Types
Activity 9.01: Creating a DeepPartial<T> Type
Solution:
Let's build this type up, step by step:
- First, let's create a
PartialPrimitive
type:type PartialPrimitive = string | number | boolean | symbol | bigint | Function | Date;
- Then, let's start by defining a basic
DeepPartial<T>
type:type DeepPartial<T> = T extends PartialPrimitive ? T : Partial<T>;
Next, we need to handle more complex structures – such as arrays, sets, and maps. These require using the
infer
keyword, and in addition to that, require some more "manual wiring" for each of these types. - Let's start with adding handling for the
Array
type:type DeepPartial<T> = T extends PartialPrimitive ? T : T extends Array<infer U> ? Array<DeepPartial<U>> ...