How are the TypeScript Partial and Required types implemented?
I find it important to know how the most-used TypeScript utility types are implemented.
Here, we see the implementation of the Partial
(all properties of a type are optional) and Required
(all properties of a type are required) types.
Knowing this is essential because it allows you to customize or create a new behavior for your use cases.
keyof Person
generates a union of the keys of Person, sokeyof Person
results in"id" | "name"
.[key in keyof Person]
iterates over each key in that union.Person[key]
is the type of the property in Person associated with the key, so "id" is a number, and "name" is a string.- The
?
modifier makes the property optional (used for thePartial
type), while the-?
modifier removes the optional modifier from the property, making it required (used for theRequired
type).
Support me
I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.
Join My Newsletter (WIP)
Join my weekly newsletter to receive my latest blog posts and bits, directly in your inbox.