Sébastien TIMONER
Expert in web development and team management, I specialize in creating and optimizing high-performance digital solutions. With extensive expertise in modern technologies like React.js, Node.js, TypeScript, Symfony, and Zephyr OS for IoT, I ensure the success of complex SaaS and IoT projects, from design to production, for companies across various sectors, at offroadLabs.
At offroadLabs, I offer custom development services that combine technical expertise with a collaborative approach. Whether creating an innovative SaaS solution, developing IoT systems with Zephyr OS, modernizing an existing application, or supporting the upskilling of a team, I am committed to delivering robust and high-performance solutions tailored to the specific needs of each project.
I am available for projects in the Aix-en-Provence area or fully remote.
TypeScript offers numerous utility types that allow you to manipulate types dynamically and practically. Whether it's making properties optional, immutable, or filtering certain fields from a type, these utilities can help you write cleaner, safer, and more readable code.
In this article, you'll discover some of the most commonly used utility types in TypeScript, with concrete examples showing you how and when to use them.
Partial<T>
The Partial<T>
utility type transforms all properties of type T
into optional properties. This is particularly useful when working with objects where all properties aren't always necessary.
Let's imagine a User
type with required properties:
typescript
If you want to create a function that updates a user but only needs a subset of these properties, you can use Partial<User>
:
typescript
Thanks to Partial<User>
, you can now update only certain properties without having to provide all the others.
Readonly<T>
Readonly<T>
makes all properties of type T
immutable. This means once the object is created, you can't modify its properties, which is ideal for constant objects.
typescript
By using Readonly<Config>
, you ensure that config
remains immutable throughout execution.
Pick<T, K>
The Pick<T, K>
utility type creates a new type by selecting only certain properties from type T
. This is useful when you want to create a subset of an existing type.
typescript
With Pick<User, "id" | "name">
, you create a UserSummary
type that contains only the id
and name
properties.
Omit<T, K>
Conversely, Omit<T, K>
creates a new type by excluding certain properties from type T
. It's the opposite of Pick
.
typescript
In this example, UserWithoutAddress
contains all properties of User
, except address
.
Record<K, T>
Record<K, T>
is used to create an object type where the keys K
are of a specific type, and the values are of type T
. This is useful for creating associative objects, like dictionaries or mappings.
Let's say you want to create an object associating user roles with access rights:
typescript
In this example, Record<Role, Permissions[]>
ensures that the rolePermissions
object contains all keys admin
, user
, and guest
, with values of type Permissions[]
.
Exclude<T, U>
Exclude<T, U>
allows you to create a type by excluding certain types from another type. It's useful for filtering specific types from a Union Type.
typescript
By using Exclude<Status, "suspended">
, you create an ActiveStatus
type that can't be "suspended"
.
TypeScript's utility types allow you to express complex types in a concise and readable way. By leveraging Partial
, Readonly
, Pick
, Omit
, Record
, and Exclude
, you can manipulate your types dynamically and meet your application's needs without cluttering your code.
These utilities are just the tip of the iceberg: TypeScript offers many other advanced utility types. Take the time to explore these tools, and your code will be more maintainable, safer, and easier to read.
By applying these tips, you'll be able to manipulate your types more effectively and write even more robust TypeScript code. Happy coding!