All essential TypeScript types in one place providing 70+ utility types and helper functions.
—
Transform specific properties of objects by changing their modifiers (optional, readonly, required, writable). These types work on individual property keys rather than entire objects.
Constructs a type by picking all properties from type Type where properties Keys are set as optional, meaning they aren't required.
type MarkOptional<Type, Keys extends keyof Type> = Prettify<
Omit<Type, Keys> & Partial<Pick<Type, Keys>>
>;Usage Example:
import type { MarkOptional } from "ts-essentials";
type User = {
id: number;
name: string;
email: string;
avatar: string;
};
type UserInput = MarkOptional<User, "id" | "avatar">;
// Result: { name: string; email: string; id?: number; avatar?: string; }
const newUser: UserInput = {
name: "Alice",
email: "alice@example.com"
// id and avatar are optional
};Constructs a type by picking all properties from type Type where properties Keys are set to readonly, meaning they cannot be reassigned.
type MarkReadonly<Type, Keys extends keyof Type> = Prettify<
Omit<Type, Keys> & Readonly<Pick<Type, Keys>>
>;Usage Example:
import type { MarkReadonly } from "ts-essentials";
type User = {
id: number;
name: string;
email: string;
lastLogin: Date;
};
type UserEntity = MarkReadonly<User, "id" | "lastLogin">;
// Result: { name: string; email: string; readonly id: number; readonly lastLogin: Date; }
const user: UserEntity = {
id: 1,
name: "Alice",
email: "alice@example.com",
lastLogin: new Date()
};
user.name = "Bob"; // OK
user.email = "bob@example.com"; // OK
// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property
// user.lastLogin = new Date(); // Error: Cannot assign to 'lastLogin' because it is a read-only propertyConstructs a type by picking all properties from type Type where properties Keys are set as required.
type MarkRequired<Type, Keys extends keyof Type> = Prettify<
Omit<Type, Keys> & Required<Pick<Type, Keys>>
>;Usage Example:
import type { MarkRequired } from "ts-essentials";
type UserPreferences = {
theme?: string;
language?: string;
notifications?: boolean;
privacy?: string;
};
type RequiredUserPreferences = MarkRequired<UserPreferences, "theme" | "language">;
// Result: { notifications?: boolean; privacy?: string; theme: string; language: string; }
const prefs: RequiredUserPreferences = {
theme: "dark", // Required
language: "en", // Required
notifications: true // Optional
// privacy is optional
};Constructs a type by picking all properties from type Type where properties Keys remove readonly modifier, meaning they can be reassigned.
type MarkWritable<Type, Keys extends keyof Type> = Prettify<
Omit<Type, Keys> & Writable<Pick<Type, Keys>>
>;Usage Example:
import type { MarkWritable } from "ts-essentials";
type ImmutableUser = {
readonly id: number;
readonly name: string;
readonly email: string;
readonly createdAt: Date;
};
type EditableUser = MarkWritable<ImmutableUser, "name" | "email">;
// Result: { readonly id: number; readonly createdAt: Date; name: string; email: string; }
const user: EditableUser = {
id: 1,
name: "Alice",
email: "alice@example.com",
createdAt: new Date()
};
user.name = "Bob"; // OK - name is now writable
user.email = "bob@example.com"; // OK - email is now writable
// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property
// user.createdAt = new Date(); // Error: Cannot assign to 'createdAt' because it is a read-only propertytype Writable<Type> = {
-readonly [Key in keyof Type]: Type[Key];
};
type Prettify<Type> = Type extends Function
? Type
: Extract<{
[Key in keyof Type]: Type[Key];
}, Type>;Install with Tessl CLI
npx tessl i tessl/npm-ts-essentials