Typescript:Utility Types
Typescript comes with some handy utility types. In this blog, we will be exploring exactly that.

Search for a command to run...
Typescript comes with some handy utility types. In this blog, we will be exploring exactly that.

OpenClaw is a powerful, self-hosted AI assistant that connects to your tools to perform actions. Explore its Gateway architecture, real-world use cases, and security precautions.

Discover how neo-brutalism is shaping 2026 design trends. See how anti-design principles can create distinct, usable, and memorable product experiences.

When code breaks a pipeline, developers have to stop working and figure out why. This blog shows how an AI agent reads the error, finds the fix, and submits it for review all on its own.

GeekyAnts built a 5-agent fraud detection pipeline that makes decisions in under 200ms — 15x cheaper than single-model systems, with full explainability built in.

A deep dive into how GeekyAnts built a real-time AI fraud detection system that evaluates transactions in milliseconds using a hybrid multi-agent approach.

GeekyAnts Tech Blog
348 posts
GeekyAnts is an AI-powered digital product engineering and consulting company helping startups, enterprises, and Fortune 500 brands build scalable, future-ready digital solutions. Since 2006, we have delivered 800+ successful projects for 550+ global clients across healthcare, BFSI, retail, logistics, education, and enterprise technology. We help businesses accelerate digital transformation through strategy, design, engineering, and AI-led innovation.
Developers frequently claim that typescript requires them to write redundant code. The reasons can vary, but one of the most common is a failure to use utility types.
Types are analogous to utility functions in Javascript.
They are provided in typescript to make our lives easier. Let's take a look at them one at a time.
Partial<T>allows you to create optional props with a single line of code. Without it, we'd need to duplicate the User type, which isn't ideal.
type User = {
name: string;
password: string;
}
// redundant codes
type OptionalUserProps = {
name?: string;
password?: string;
}
Example of Partial utility type.
type OptionalUsersProps = Partial<User>
Partial takes type as an argument.
Required<T> is the inverse of Partial. It creates all the necessary props.
type Fish {
name: string;
food?: string[];
origin?: string;
}
// all the props are required.
type RequiredFish = Required<Fish>
Required also takes type as an argument.
In JavaScript, readonly functions similarly to const. You cannot reassign a type once it has been marked as readonly.
type Currency = {
America: "USD";
India: "INR";
France: "Euro";
}
type ReadOnlyCurrency = Readonly<Currency>;
NonNullable<T> filters your type and removes null and undefined from the union of types.
// gender will have male and female type.
type Gender = NonNullable<"male" | "female" | null | undefined>;
Exclude<T, K> lets you remove type from the union types.
type Languages = "Java" | "JavaScript" | "TypeScript";
// Excluding Java from union type.
type MyFavLang = Exclude<Languages, "Java">;
// passing union type.
type TypeScriptOnly = Exclude<Languages, "Java" | "JavaScript">
It takes two arguments. The first is type, while the second is a union of props.
Extract<T, K>is the inverse of Exclude. It will generate union types based on the specified union type.
type Languages = "Java" | "JavaScript" | "TypeScript";
// Extract TypeScript and JavaScript from Languages.
type MyFavLang = Extract<Languages, "TypeScript" | "JavaScript">;
Pick<T, K> picks props from type. It will give you a new type.
type Car {
engine: Engine;
wheels: Wheels;
color: "red" | "white" | "black";
speed: number;
}
// selecting engine and wheels only.
type CarService = Pick<Car, "engine" | "wheels">
Omit<T, K> removes props from types.
type NewUser = {
name: string;
username: string;
password: number;
confirmPassword: number;
}
// password and confirmPassword will be removed
type User = Omit<NewUser, "password", "confirmPassword">
ReturnType<T> gives a return type of functions.
// Type T can be string or number
type T = ReturnType<() => string | number>
Record<T, K>creates an object type.
Instead of doing this:
type SpaceShip = {
[k :string]: number;
}
You can do this:
type SpaceShip = Record<string, number>
const spaceship: SpaceShip = {
name: "StarShip",
color: "Red",
speed: "High"
}
// You can pass union type as key as well.
type Num = Record<string | number, number>
const numbers :Num = {
first: 1,
2: 2,
three: 3
}
Record also accepts union type as key that is not supported by
interfaceandtype.
As we can see here, utility types helps us avoid redundancy in the codebase. We can also maintain a single source of truth.
That's it for today. I hope you enjoyed this blog. Stay tuned for upcoming blogs on the typescript.