Typescript - Tips & Tricks - Type Guard

Full Stack Developer and JavaScript and TypeScript enthusiastic.
Search for a command to run...

Full Stack Developer and JavaScript and TypeScript enthusiastic.
No comments yet. Be the first to comment.
In this series, You'll see some tips and tricks using Typescript
In some cases, we need to detect if an object/type has specific properties or characteristics. Let me show you a simple case type TypeName<T> = T extends string ? "string" : T extends number ? "number" : T extends bigint ? "bigint" : ...
Today, I wanna show you how to set up your devices with Antigravity using only the agentic mode, without touching any code. At the last CodeMotion Rome, I got my special badge. A CM Node, a special ha

As a developer, you have to take control of your projects every day. Whether it is a company repository, an open source project you maintain or collaborate on, or a simple pet project. Gaining control over your projects often depends on the platform ...

My Antigravity first experience

Write clear and insightful commit messages using AI and GitLens.

We are in the AI era. New models emerge daily, and many applications have already integrated AI into their workflows. Gemini, OpenAI, Copilot, Deepseek, Llama, and many others enable AI in your applications or help you write code, but they all need a...

There are some cases, where we need to detect the type of the object to get the correct implementation of our method. Let me show you a case
type Square = {
size: number;
};
type Rectangle = {
with: number;
height: number;
};
type Circle = {
radius: number;
};
type Shape = Square | Rectangle | Circle;
const area = (shape: Shape) => {
if ("size" in shape) return shape.size * shape.size;
if ("with" in shape) return shape.with * shape.height;
if ("radius" in shape) return shape.radius * 2 * Math.PI;
};
In this case, in the area method, we need to detect the type of the shape to return the correct result. Here we detect the type of the shape in the if condition but typescript permits us to create a special function that identifies if a type is of a specific type or not. These functions are created to have Type Guards that help the compiler, the intellisense, and us to understand the correct type of the object. To show you this function in action, the previous example can be rewritten in this way.
Square = {
size: number;
};
type Rectangle = {
with: number;
height: number;
};
type Circle = {
radius: number;
};
type Shape = Square | Rectangle | Circle;
function isSquare(shape: Shape): shape is Square {
return "size" in shape;
}
function isRectangle(shape: Shape): shape is Rectangle {
return "with" in shape;
}
function isCircle(shape: Shape): shape is Circle {
return "radius" in shape;
}
const area = (shape: Shape) => {
if (isSquare(shape)) return shape.size * shape.size;
if (isRectangle(shape)) return shape.with * shape.height;
if (isCircle(shape)) return shape.radius * 2 * Math.PI;
};
As you can see, the methods: isSquare, isRectangle, and isCircle get a shape as parameter and return a boolean value. If the result is true the shape parameter corresponds to the type indicated to the right of the is keyword, otherwise, it corresponds to another type.
I hope this feature will help you in the future and improve the reading of your code.
It's all Guy! See you soon!