NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • Partial<T>
  • Required<T>
  • Pick<T, K>
  • Omit<T, K>
  • Record<K, T>
  • Exclude<T, U>
  • Extract<T, U>
  • NonNullable<T>
  • ReturnType<T>
Edit on GitHub
Export as PDF
  1. Orbital
  2. TypeScript

Utility Types

Utility types are built-in TypeScript helpers that transform types. They make your types more concise, flexible, and powerful, especially when working with component props, APIs, and state.

We’ll cover the key TypeScript types you’ll encounter and use regularly when writing React code.

Partial<T>

Makes all properties optional.

// Define the original full type
interface User {
  id: string;
  name: string;
  age: number;
}

// Use Partial to make all fields optional
type UpdateUser = Partial<User>;

// You can now create an object with just some fields
const updatedUserInfo: UpdateUser = {
  name: "Bob", // no need for id or age
};

// Simulate an update function where not all user data needs to change
const updateUser = (user: UpdateUser): void => {
  // This function accepts any subset of the User fields
  console.log("Updating user with:", user);
};

updateUser(updatedUserInfo); // Updating user with: { name: "Bob" }

Use Partial when you want to allow updating or creating objects without needing all fields up front.

Required<T>

Makes all properties required (the opposite of Partial<T>).

interface Props {
  name?: string;
  age?: number;
}

type AllRequired = Required<Props>;

const person: AllRequired = {
  name: "Bob",
  age: 42,
};

Pick<T, K>

“Give me a type with only these specific keys from the original.”

Creates a type by picking a subset of properties from another type.

interface User {
  id: string;
  name: string;
  age: number;
  email: string;
  isAdmin: boolean;
}

// Pick only id and name
type UserPreview = Pick<User, "id" | "name">;

const userPreview: UserPreview = {
  id: "123",
  name: "Alice",
};

Omit<T, K>

The inverse of Pick<T>. Removes specific properties from a type.

interface User {
  id: string;
  name: string;
  email: string;
  isAdmin: boolean;
}

type UserWithoutAdmin = Omit<User, "isAdmin">;

const user: UserWithoutAdmin = {
  id: "1",
  name: "Alice",
  email: "alice@example.com",
};

Record<K, T>

Constructs an object type where the keys K have values of type T.

type Status = "idle" | "loading" | "error";

type StatusMessages = Record<Status, string>;

const messages: StatusMessages = {
  idle: "Nothing happening",
  loading: "Loading...",
  error: "Something went wrong",
};

Exclude<T, U>

Removes from T the types that are assignable to U.

type Roles = "admin" | "user" | "guest";

type NonGuest = Exclude<Roles, "guest">;
// "admin" | "user"

Extract<T, U>

Keeps only types from T that are assignable to U.

type AllRoles = "admin" | "user" | "guest";
type StaffRoles = Extract<AllRoles, "admin" | "user">;
// "admin" | "user"

NonNullable<T>

Removes null and undefined from a type.

type MaybeName = string | null | undefined;
type Name = NonNullable<MaybeName>; // string

ReturnType<T>

Gets the return type of a function.

function getUser() {
  return {
    id: "1",
    name: "Alice",
  };
}

type UserReturn = ReturnType<typeof getUser>;
// { id: string; name: string }

PreviousTypes and InterfacesNextTyping Component Props, Events, and Hooks

Last updated 2 days ago

More can be found in the .

official documentation