Typescript - Tips & Tricks - Never type

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
There are times that we need to create some assertions to be sure an object respects some rules. A common case is to check if an object is defined; to do this you can create a simple assertion function like this function assert(condition: unknown, me...
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...

Today I want to talk about the never type. As you can understand, this type identifies a piece of code that will never be executed or represents a state that shouldn’t exist. Here an example
const throwException: () => never = () => {
throw new Error();
};
throwException()
console.log('never called');
In this simple example, you can see how the "throwException" method never permits the code execution of the console.log. In this case, the "throwException" method returns the never type because the method doesn't return any value but throws an error. To better understand the never type I show you another simple example
type Square = {
kind: "square";
size: number;
};
type Rectangle = {
kind: "rectangle";
with: number;
height: number;
};
type Circle = {
kind: "circle";
radius: number;
};
type Shape = Square | Rectangle | Circle;
const area = (shape: Shape): number => {
if (shape.kind === "square") return shape.size * shape.size;
if (shape.kind === "rectangle") return shape.with * shape.height;
// if (shape.kind === "circle") return shape.radius * 2 * Math.PI;
const _exhaustiveCheck: never = shape; // Type 'Circle' is not assignable to type 'never'.
return _exhaustiveCheck;
};
In this example, you can see how the "area" method detects the kind of the shape and it calculates the relative area. I leave the circle type comment voluntarily, so I can show you the power of the never type. At the end of the area method, you can see how the code assigns the shape to the field "_exhaustiveCheck" of type never. In this case, the typescript language detects that if you pass a circle shape at the area method, the line "const _exhaustiveCheck: never = shape;" will be executed, so it notifies you raising an error. The typescript language detects that the never type could be executed at runtime so it tries to prevent this problem. To fix this problem the previous method can be reviewed in this way
type Square = {
kind: "square";
size: number;
};
type Rectangle = {
kind: "rectangle";
with: number;
height: number;
};
type Circle = {
kind: "circle";
radius: number;
};
type Shape = Square | Rectangle | Circle;
const area = (shape: Shape): number => {
if (shape.kind === "square") return shape.size * shape.size;
if (shape.kind === "rectangle") return shape.with * shape.height;
if (shape.kind === "circle") return shape.radius * 2 * Math.PI;
const _exhaustiveCheck: never = shape;
return _exhaustiveCheck;
};
Now the code is compiled successfully and the never type is assigned correctly.
From the never type it's all. See you soon guys