Type Transformations Workshop (4 exercises)

Construct a Type with Optional Properties

Partials allow you to construct types where some of the higher-level properties are optional. The idea of a DeepPartial takes this further by allowing all of the properties to become optional.

This is useful for when you want to be able to include just some parts of a type.

Consider MyType below, which has several properties at various levels:


Your challenge is to create a DeepPartial type helper that takes in MyType and returns a partial version of it. Read more about the Partial Utility type in the TypeScript Docs to learn more.

In other words, all of the members of the object will be undefined, including those inside of other objects.

Something that may be helpful is that when you're inside of a type, you can actually reuse that type. As you work on your DeepPartial, you are able to recursively call DeepPartial.

Conditional and mapped types will be in your solution, as well as some other TypeScript keywords we’ve used throughout the workshop.


[0:00] [0: 00] In this exercise, we're going to be creating a deep partial type helper, which takes in a type here and returns a partial version of that type. In other words, all of the members of the objects are going to be undefined, and even works inside arrays and inside other objects too. This should work to any depth, basically. It should keep recursively going down and down.
[0:28] [0: 28] One thing that might help you here is thinking that, inside a type, you can reuse that type if you want to. You're probably going to need a conditional type here. We're going to be wanting to recursively call deep partial further and further down the tree as we go.

[0:46] [0: 47] This one's going to be tricky conceptually to get your head around, but we're going to need a conditional type, probably going to need a partial type helper too. There is maybe one thing that you haven't seen before. Do check in the docs around map types for that. Best of luck.