A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/typescript/typescript-utility-types/ below:

TypeScript Utility Types - GeeksforGeeks

TypeScript Utility Types

Last Updated : 23 Jul, 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<Type>

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<T>
JavaScript
interface User {
    Id: string;
    email: string;
}

type PartialUser = Partial<User>;

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

console.log(partialUser);

Output:

{ Id: '123' }
2. Required<Type>

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<T>
JavaScript
interface User {
    name?: string;
    age?: number;
}

type RequiredUser = Required<User>;

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

console.log(requiredUser);

Output:

{ name: 'John', age: 20 }
3. Readonly<Type>

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<T>
JavaScript
interface User {
    name: string;
    age: number;
}

type ReadonlyUser = Readonly<User>;

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>
JavaScript
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<Type>

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<T>
JavaScript
function sum(a: number, b: number): number {
    return a + b;
}

type SumParams = Parameters<typeof sum>;
// 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>
JavaScript
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>
JavaScript
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:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4