tsafe
GitHub
  • 👋What is tsafe?
  • ⚙️How to import
  • assert
  • Equals
  • id
  • is
  • objectKeys
  • exclude
  • isAmong
  • symToStr
  • ReturnType
  • Parameters
  • Param0
  • typeGuard
  • capitalize/uncapitalize
  • MethodNames
  • isPromiseLike
  • flip
  • objectEntries
  • objectFromEntries
  • UnionToIntersection
  • 🚧withDefaults
  • 📉UnpackPromise
Powered by GitBook
On this page
  • Example 1: Simultaneously declaring a type and instantiating a default value for this type
  • Example 2: Instantiating an object of type T

Was this helpful?

Edit on GitHub

id

The identity function

PreviousEqualsNextis

Last updated 6 months ago

Was this helpful?

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 }));
Cat["gender"] is "male" | "female"
Cat["gender"] is string
"MALE" is all caps, which should be a typing error
Every possible properties are listed
TypeScript knows we are instantiating a Circle