13 Typescript Utility: A Cheat Sheet for Developer

Rahul Sharma (DevsMitra)
4 min readApr 7, 2022
13 Typescript Utility: A Cheat Sheet for Developer | Rahul Sharma(DevsMitra)
13 Typescript Utility: A Cheat Sheet for Developer

Typescript is very powerful in terms of type checking, but sometimes it gets tedious when some types are subsets of other types and you need to define type checking for them.

Let's take an example, you have 2 response types:

UserProfileResponse

interface UserProfileResponse {
id: number;
name: string;
email: string;
phone: string;
avatar: string;
}

LoginResponse

interface LoginResponse {
id: number;
name: string;
}

Instead of defining types of the same context LoginResponse and UserProfileResponse, we can define the type for UserProfileResponse and pick some properties for LoginResponse.

type LoginResponse = Pick<UserProfileResponse, "id" | "name">;

Let’s understand some utility functions that can help you to write better code.

Uppercase

Constructs a type with all properties of Type set to uppercase.

type Role = "admin" | "user" | "guest";// Bad practice 💩
type UppercaseRole = "ADMIN" | "USER" | "GUEST";
// Good practice ✅
type UppercaseRole = Uppercase<Role>; // "ADMIN" | "USER" | "GUEST"

--

--

Rahul Sharma (DevsMitra)
Rahul Sharma (DevsMitra)

Written by Rahul Sharma (DevsMitra)

I’m a technology enthusiast who does web development. Passionate to contribute to open-source projects and make cool products.

Responses (12)