Developer bit

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...

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, so keyof 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 the Partial type), while the -? modifier removes the optional modifier from the property, making it required (used for the Required type).
partial.ts
type Person = { id: number, name: string }
// 🪄 Use the ? modifier to make a property optional
type PersonPartial = { [key in keyof Person]?: Person[key] };
const person: PersonPartial = { name: 'Tim' }
// ^? { id?: number | undefined; name?: string | undefined }
// 🔁 Refactor into a generic type
type GenericPartial<T> = { [key in keyof T]?: T[key] };
const person: GenericPartial<Person> = { name: 'Tim' }
// ^? { id?: number | undefined; name?: string | undefined }
// Use the built-in Partial Type:
// Constructs a type with all properties of Type
// set to optional. This utility will return a type that represents
// all subsets of a given type.
const person: Partial<Person> = { name: 'Tim' }
// ^? { id?: number | undefined, name?: string | undefined }

Feel free to update this developer bit on GitHub, thanks in advance!

Enjoying the blog?

Support my work

If you enjoyed this post and found it useful, consider supporting my work. It helps me keep creating and sharing content like this. Thank you!