# id

Literally just:

```typescript
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

```typescript
import { id } from "tsafe/id";

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

type Cat = typeof defaultCat;
```

![Cat\["gender"\] is "male" | "female"](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_DPR4G1FRgI3DwBvcp%2F-M_GwGbKoPJ38W1Q7FpC%2F-M_HBMeFPs9A_CwsCpDA%2Fimage.png?alt=media\&token=dfdf8735-73a9-440f-a900-5bccb2f9747a)

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

![Cat\["gender"\] is string](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_DPR4G1FRgI3DwBvcp%2F-M_GwGbKoPJ38W1Q7FpC%2F-M_HCRa0l6xqVPUT8WdG%2Fimage.png?alt=media\&token=3ba73fb6-9fa8-4175-be17-51f206787d15)

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

```typescript
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:

!["MALE" is all caps, which should be a typing error](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_DPR4G1FRgI3DwBvcp%2F-M_GwGbKoPJ38W1Q7FpC%2F-M_HE-wpMZBjlTwDEPtl%2Fimage.png?alt=media\&token=1eed5180-deae-43bf-991d-7b14d32b5ae8)

## Example 2: Instantiating an object of type T

Let's say you have this function:

```typescript
declare function getArea(shape: Shape): number;
```

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

```typescript
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:

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

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

```typescript
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:

![Every possible properties are listed](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_DPR4G1FRgI3DwBvcp%2F-M_GwGbKoPJ38W1Q7FpC%2F-M_H2tt2Vgu3xQa04ezL%2Fimage.png?alt=media\&token=7baa15c3-ec23-4c0e-b820-4490143ccbc1)

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

```typescript
import { id } from "tsafe/id";

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

![TypeScript knows we are instantiating a Circle](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_DPR4G1FRgI3DwBvcp%2F-M_GwGbKoPJ38W1Q7FpC%2F-M_H5_VFU4s5qgm-9Acv%2Fimage.png?alt=media\&token=3e7c2f5f-de35-4342-b24e-4c0c67379166)
