Frontend
ยท2 min read

3 Ways of Type Transformation in Typescript

Main cover illustration for article: 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.

Error compilers missing properties
Error compilers missing properties

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:

Partial makes 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

Read more articles in www.danywalls.com Foto de Suzi Kim en Unsplash

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


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.

User avatar
User avatar
User avatar
User avatar
+13K

Join 13,800+ developers and readers.

No spam ever. Unsubscribe at any time.

Discussion