id

The identity function

Literally just:

export const id = <T>(x: T) => x;

It directly returns the parameter it was given as input.

Example 1: Simultaneously declaring a type and instantiating a default value for this type

import { id } from "tsafe/id";

const defaultCat = {
	name: "Felix",
	gender: id<"male" | "female">("male"),
};

type Cat = typeof defaultCat;

If we don't use id, Cat["gender"] is of type string

We could have used "male" as "male" | "female"

const defaultCat = {
	name: "Felix",
	gender: "male" as "male" | "female",
};

type Cat = typeof defaultCat;

But this is less type safe because we do not validate that the value that we gives to gender is actually assignable to "male" | "female".

This error for example slips through:

Example 2: Instantiating an object of type T

Let's say you have this function:

declare function getArea(shape: Shape): number;

And let's say a shape object is defined as follows:

type Circle = { type: "circle"; radius: number };
type Square = { type: "square"; sideLength: number };
type Shape = Circle | Square;

We want to instantiate a Circle and pass it to getArea we can do:

const circle: Circle = { type: "circle", radius: 33 };
getArea(circle);

If we want to avoid declaring a variable, we can do

getArea({ type: "circle", radius: 33 });

The problem, however, is that this Circle was not as easy to instantiate because TypeScript doesn’t know what kind of shape we are trying to instantiate:

id lets you declare that the shape you are instantiating is a Circle

import { id } from "tsafe/id";

getArea(id<Circle>({ type: "circle", radius: 33 }));

Last updated