3 Ways of Type Transformation in Typescript

When we use types or interfaces, the typescript compiler enforces the object fit with them to avoid runtime errors for missing fields.
Sometimes we want the flexibility to create an object without breaking the contract with the interface type.
For example, we have the interface User with all properties required.
interface User {
username: string;
password: string;
roles: string[];
active: boolean;
}Maybe we want to create a user and later call the API to get the roles. If we try to make the object without all the properties, the compiler shows an error because it doesn't fit with the interface.
In most cases, we change the property to optional roles?: string[] and to allow compiling, but we know it is a cheat.
Typescript provides the utility to work with it:
Partial: Create a new type with all properties as optional from the type:
let admin: Partial<User> = {
username: 'admin',
password: 'admin'
}Thanks to @lukeshiru contribution, I learn other ways to deal with it:
Partialmakes every property optional. Maybe it is not what we want.
Typescript provides other Utility types, Omit and Pick let play with it.
Omit: Create a new type taking all properties from the type and removing the keys.
type UserWithoutRoles = Omit<User, "roles">;
const userWithoutRoles: UserWithoutRoles = {
username: "admin",
password: "admin",
};Pick: Create a new type from the type and pick a set of properties.
type UserWithStatus = Pick<User, "username" | "active">
const userWithStatus: UserWithStatus = {
username: "dany",
active: true
}Thanks again to @lukeshiru :)
Learn more about TypeScript Utility Types. Once you transform your types, you may also need to validate them at runtime—check out my in-depth guide on how to check types in TypeScript to master type narrowing and custom type guards.
Read more articles on danywalls.com. Foto de Suzi Kim en Unsplash
Modern frontend development — Angular, React, TypeScript, accessibility, and performance.
Frequently Asked Questions
What is the TypeScript Partial utility type and when should I use it?
Partial is a TypeScript utility type that creates a new type where all properties of the original type become optional. You use it when you need to create an object that only has some of the fields defined in an interface, such as when building a partial update payload or when constructing an object incrementally before all data is available.
What is the difference between Omit and Pick in TypeScript?
Omit creates a new type by taking all properties from an existing type and removing the ones you specify by key name. Pick creates a new type by selecting only the specific properties you name. Use Omit when you want most of the type but need to exclude a few fields, and use Pick when you only need a small subset of properties from a larger type.
How do I create a TypeScript type that excludes certain properties from an interface?
Use the Omit utility type by writing Omit followed by the source type and a union of the property names you want to exclude. For example, Omit with a User type and the string roles will produce a new type that has all User properties except roles. The result is a fully typed object that the compiler will validate like any other type.
Why should I use TypeScript utility types instead of making properties optional with a question mark?
Making a property optional with a question mark changes the original interface contract permanently, which can mask bugs elsewhere in the codebase where that property is expected to be present. Utility types like Partial, Omit, and Pick let you create derived types for specific use cases without modifying the base interface, keeping your types precise and your code safer.
Related Articles
How to Check Types in TypeScript (The Complete Guide)
Learn how to check and validate types in TypeScript at compile-time and runtime using typeof, instanceof, the in operator, discriminated unions, custom type guards, and Zod or Valibot.
Abstract Classes in Typescript
In Typescript, the classes can inherit from another class to share methods and properties between classes. Also, Typescript support abstract class. Let me show you why and when to use it. For example, we have a base class Subscription and create the ...
Difference between 'extends' and 'implements' in TypeScript
Today, a friend ask about the difference between extends and implements. class Media { format: string; } class Video extends Media {} class Image implements Media {} The short answer for him was: extends: The class get all these methods and proper...
How To Test Private Methods in Typescript
Sometimes we need to test a private method, and Yes, maybe it's not a best practice, but in my case, I have a situation when I need to add a test to my private method, and if you have the same situation I hope it helps you. My example is a service wi...
Share this article
If you found this guide helpful, consider sharing it with your team or fellow developers.
Real Software. Real Lessons.
I share the lessons I learned the hard way, so you can either avoid them or be ready when they happen.
Join 13,800+ developers and readers.
No spam ever. Unsubscribe at any time.