

Creating Your Own DeepPartial Type in Typescript to Allow Any Subset of Object P...
source link: https://typeofnan.dev/creating-your-own-deeppartial-type-in-typescript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

TypeOfNaN
Creating Your Own DeepPartial Type in Typescript to Allow Any Subset of Object Properties Deeply
Nick Scialli • September 26, 2020 • 🚀 3 minute read
You may know of the Partial utility type in Typescript, but it turns out we can create our own DeepPartial type through some clever recursive typing.
TL;DR
If you just want the type, here it is! If you want to read more about the problem, read on!
type DeepPartial<T> = Partial<{ [P in keyof T]: DeepPartial<T[P]> }>;
What is DeepPartial Type
Let’s say you have an object with properties that each have their own objects. If we use the Partial
type, we are only allowing a subset of the highest level object’s keys, but not the lower level object.
If we have the following type:
type LivingThings = {
animals: {
mammals: {
dogs: 'happy';
cats: 'annoyed';
};
};
plants: {
trees: 'tall';
ferns: 'short';
};
};
And we tried to make the following typing work:
const dogs: Partial<LivingThings> = {
animals: {
mammals: {
dogs: 'happy',
},
},
};
It would fail!
That’s because we’re allowed to have a partial object at the top level, but down within the animals
and mammals
keys, we have to include everything.
This is where a deep partial comes in handy!
Writing the DeepPartial Type
Our DeepPartial
type will also take a generic to specify the object. However, instead of just being a Partial
of our object, it will be a Partial
of our object whose keys are each mapped to a DeepPartial
of themselves!
See what I mean below:
type DeepPartial<T> = Partial<{ [P in keyof T]: DeepPartial<T[P]> }>;
Now, our DeepPartial
typing works!
const dogs: DeepPartial<LivingThings> = {
animals: {
mammals: {
dogs: 'happy',
},
},
};

Nick Scialli is a software engineer at the U.S. Digital Service.
Subscribe to the mailing list!
If you like what I post here, please sign up to get updates and code insights in your inbox. I won't spam you and you can unsubscribe any time!
Enter your emailRecommend
-
52
Digital Minimalism for the Working Hacker 2018-01-14 Effectively speaking, I’ve always been a Vim user. When I got my first shell account, I started with Vim (In the Pale...
-
28
You’re putting the finishing touches on your new million-dollar-idea — your copy is perfect, your color scheme is dazzling, and you’ve found a glorious font pairing (who knew Baskerville and Raleway looked so great togeth...
-
55
SubX: A minimalist assembly language for a subset of the x86 ISA SubX is a simple, minimalist stack for programming your computer. $ git clone https://github.com/akkartik/mu $ cd mu/subx $ ./subx #...
-
10
-
12
Quick Tip - Allow A Null Value For An Object That Doesn't Normally Allow It In the PowerShell Slack channel (powershell.slack.com) a question came up along the lines of “I have...
-
11
Mugo, a toy compiler for a subset of Go that can compile itself April 2021 Summary: This article presents Mugo, a single-pass compiler for a tiny subset of the Go programming language. It outputs (very naive) x...
-
9
Leetcode 368. Largest Divisible Subset 当前位置:XINDOO > 算法 > 正文 题目链接:
-
9
petite-vue petite-vue is an alternative distribution of Vue optimized for progressive enhancement. It provides the same template syntax and reactivity mental model with standard Vue. However, it is specifically optimized...
-
5
While working with javascript objects, often, there is a requirement to get a subset of the properties of a javascript object. This article will discuss different ways to get a subset of properties of one object into a new object using variou...
-
8
Friday, December 8, 2023 Allow object_id as a column name for Active Record and a lot more! Posted by Emmanuel Hayford
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK