TypeScript Utility Types (original) (raw)

Last Updated : 24 Jan, 2025

TypeScript utility types are predefined constructs that facilitate common type transformations, enhancing code flexibility and maintainability.

Here is the list of utility types in TypeScript:

1. Partial

Partial utility type constructs a type with all properties of the provided type T set to optional. It's useful when we want to create a new type where all properties can be present but aren't required.

Syntax:

Partial

interface User { Id: string; email: string; }

type PartialUser = Partial;

const partialUser: PartialUser = { Id: '123' };

console.log(partialUser);

`

**Output:

{ Id: '123' }

2. Required

In contrast to Partial, Required utility type constructs a type where all properties of the provided type T are set to required. This is useful when we want to ensure that all properties must be present.

Syntax:

Required

interface User { name?: string; age?: number; }

type RequiredUser = Required;

const requiredUser: RequiredUser = { name: 'John', age: 20 };

console.log(requiredUser);

`

**Output:

{ name: 'John', age: 20 }

3. Readonly

Readonly utility type constructs a type where all properties of the provided type T are marked as readonly. It's beneficial for ensuring immutability.

Syntax:

Readonly

JavaScript `

interface User { name: string; age: number; }

type ReadonlyUser = Readonly;

const readonlyUser: ReadonlyUser = { name: 'John', age: 30 }; readonlyUser.name = 'Jane'; // This line will giv error: 'name' is read-only

`

**Output:

error: Cannot assign to 'name' because it is a read-only property.

4. Pick<Type, Keys>

Pick utility type constructs a type by picking the set of properties specified by the keys K from the type T. It's useful when we want to create a new type by selecting only a few properties from an existing type.

Syntax:

Pick<T, K>

interface User { name: string; age: number; email: string; }

type UserSummary = Pick<User, 'name' | 'email'>;

const userSummary: UserSummary = { name: 'ram', email: 'ram@example.com' }; console.log(userSummary)

`

**Output:

{ name: 'ram', email: 'ram@example.com' }

5. Parameters

Parameters utility type obtains the parameter types of a function type T as a tuple type. It's beneficial when we need to extract the parameter types of a function for further type manipulation.

Syntax:

Parameters

function sum(a: number, b: number): number { return a + b; }

type SumParams = Parameters; // SumParams is [number, number]

const params: SumParams = [1, 2];

console.log(params);

`

**Output:

[1, 2]

6. Record<Keys, Type>

Record utility type constructs an object type whose property keys are of type K and values are of type T. It's useful when you want to define a fixed set of keys with specific value types.

Syntax:

Record<K, T>

type Fruit = 'apple' | 'banana' | 'orange'; type Inventory = Record<Fruit, number>;

const inventory: Inventory = { apple: 10, banana: 15, orange: 20 }; console.log(inventory)

`

**Output:

{ apple: 10, banana: 15, orange: 20 }

7. Exclude<Type, ExcludedUnion>

The Exclude utility type constructs a new type by excluding from T all properties that are assignable to U.

Syntax:

Exclude<T, U>

type Status = 'pending' | 'approved' | 'rejected'; type NonRejectedStatus = Exclude<Status, 'rejected'>;

const status: NonRejectedStatus = 'approved';

console.log(status);

`

**Output:

approved

Best Practices for Using TypeScript Utility Types: