13 Typescript Utility: A Cheat Sheet for Developer

Rahul Sharma - Apr 2 '22 - - Dev Community

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.

13 Typescript Utility: A Cheat Sheet for Developer

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

UserProfileResponse
interface UserProfileResponse {
  id: number;
  name: string;
  email: string;
  phone: string;
  avatar: string;
}
Enter fullscreen mode Exit fullscreen mode
LoginResponse
interface LoginResponse {
  id: number;
  name: string;
}
Enter fullscreen mode Exit fullscreen mode

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

type LoginResponse = Pick<UserProfileResponse, "id" | "name">;
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Lowercase

Constructs a type with all properties of Type set to lowercase. Opposite of Uppercase.
type Role = "ADMIN" | "USER" | "GUEST";

// Bad practice πŸ’©
type LowercaseRole = "admin" | "user" | "guest";

// Good practice βœ…
type LowercaseRole = Lowercase<Role>; // "admin" | "user" | "guest"
Enter fullscreen mode Exit fullscreen mode

Capitalize

Constructs a type with all properties of Type set to capitalize.
type Role = "admin" | "user" | "guest";

// Bad practice πŸ’©
type CapitalizeRole = "Admin" | "User" | "Guest";

// Good practice βœ…
type CapitalizeRole = Capitalize<Role>; // "Admin" | "User" | "Guest"
Enter fullscreen mode Exit fullscreen mode

Uncapitalize

Constructs a type with all properties of Type set to uncapitalize. Opposite of Capitalize.
type Role = "Admin" | "User" | "Guest";

// Bad practice πŸ’©
type UncapitalizeRole = "admin" | "user" | "guest";

// Good practice βœ…
type UncapitalizeRole = Uncapitalize<Role>; // "admin" | "user" | "guest"
Enter fullscreen mode Exit fullscreen mode

Partial

Constructs a type with all properties of Type set to optional.
interface User {
  name: string;
  age: number;
  password: string;
}

// Bad practice πŸ’©
interface PartialUser {
  name?: string;
  age?: number;
  password?: string;
}

// Good practice βœ…
type PartialUser = Partial<User>;
Enter fullscreen mode Exit fullscreen mode

Required
Constructs a type consisting of all properties of Type set to required. Opposite of Partial.
interface User {
  name?: string;
  age?: number;
  password?: string;
}

// Bad practice πŸ’©
interface RequiredUser {
  name: string;
  age: number;
  password: string;
}

// Good practice βœ…
type RequiredUser = Required<User>;
Enter fullscreen mode Exit fullscreen mode

Readonly

Constructs a type consisting of all properties of Type set to readonly.
interface User {
  role: string;
}

// Bad practice πŸ’©
const user: User = { role: "ADMIN" };
user.role = "USER";

// Good practice βœ…
type ReadonlyUser = Readonly<User>;
const user: ReadonlyUser = { role: "ADMIN" };
user.role = "USER"; // Error: Cannot assign to 'role' because it is a read-only property.
Enter fullscreen mode Exit fullscreen mode

Record

Constructs a type with a set of properties K of type T. Each property K is mapped to the type T.
interface Address {
  street: string;
  pin: number;
}

interface Addresses {
  home: Address;
  office: Address;
}

// Alternative βœ…
type AddressesRecord = Record<"home" | "office", Address>;
Enter fullscreen mode Exit fullscreen mode

Pick

Pick only the properties of Type whose keys are in the union type keys.
interface User {
  name: string;
  age: number;
  password: string;
}

// Bad practice πŸ’©
interface UserPartial {
  name: string;
  age: number;
}

// Good practice βœ…
type UserPartial = Pick<User, "name" | "age">;
Enter fullscreen mode Exit fullscreen mode

Omit

Omit only the properties of Type whose keys are in the union type keys.
interface User {
  name: string;
  age: number;
  password: string;
}

// Bad practice πŸ’©
interface UserPartial {
  name: string;
  age: number;
}

// Good practice βœ…
type UserPartial = Omit<User, "password">;
Enter fullscreen mode Exit fullscreen mode

Exclude

Constructs a type with all properties of Type except for those whose keys are in the union type Excluded.
type Role = "ADMIN" | "USER" | "GUEST";

// Bad practice πŸ’©
type NonAdminRole = "USER" | "GUEST";

// Good practice βœ…
type NonAdmin = Exclude<Role, "ADMIN">; // "USER" | "GUEST"
Enter fullscreen mode Exit fullscreen mode

Extract

Constructs a type with all properties of Type whose keys are in the union type Extract.
type Role = "ADMIN" | "USER" | "GUEST";

// Bad practice πŸ’©
type AdminRole = "ADMIN";

// Good practice βœ…
type Admin = Extract<Role, "ADMIN">; // "ADMIN"
Enter fullscreen mode Exit fullscreen mode

NonNullable

Constructs a type with all properties of Type set to non-nullable.
type Role = "ADMIN" | "USER" | null;

// Bad practice πŸ’©
type NonNullableRole = "ADMIN" | "USER";

// Good practice βœ…
type NonNullableRole = NonNullable<Role>; // "ADMIN" | "USER"
Enter fullscreen mode Exit fullscreen mode

Must Read If you haven't

Catch me on

Youtube Github LinkedIn Medium Stackblitz Hashnode HackerNoon

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .