Type-fest for TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Type-fest and why is it needed?



Type-fest is a collection of utility types for TypeScript, created by the community Sindre Sorhus. This library provides dozens of ready-made types that solve everyday tasks when developing in TypeScript: from simple transformations (e.g., making all object fields optional) to complex conditional types (e.g., extracting the value type from a Promise).



Why is this library needed? TypeScript is a powerful language, but its built-in types (Partial, Required, Pick, Record) cover only basic scenarios. Real projects often require more specific constructs: deep object merging, types for working with JSON, strict typing of constants, extracting types from complex structures, etc. Type-fest eliminates the need to write these types manually, saving time and reducing the likelihood of errors.



The library is actively maintained, has over 10,000 stars on GitHub, and is used in major projects such as Next.js, Vite, and Expo.



Installation



Type-fest is an npm package that is installed as a dev dependency (since types do not affect runtime).



# npmnpm install --save-dev type-fest

# yarnyarn add --dev type-fest

# pnpmpnpm add --save-dev type-fest


Note: the library is not installed via pip — it is intended exclusively for TypeScript/JavaScript.



Quick Start



After installation, you can import any type from the library. Let's look at a simple example — using the PartialDeep type, which makes all fields of an object (including nested ones) optional.



import type { PartialDeep } from 'type-fest';

interface User { id: number; name: string; address: { city: string; zip: number; };}

// Regular Partial makes only first-level fields optionaltype PartialUser = Partial<User>;// { id?: number; name?: string; address?: { city: string; zip: number; } }

// PartialDeep makes all nested fields optionaltype DeepPartialUser = PartialDeep<User>;// { id?: number; name?: string; address?: { city?: string; zip?: number; } }

const user: DeepPartialUser = { name: 'Alice', address: { city: 'Wonderland' // zip can be omitted }};


As you can see, PartialDeep automatically traverses all nesting levels — something you would have to write manually using recursive types.



Main Methods/Types



Let's look at the 10 most useful types from Type-fest. For each, a description, parameters (if any), and a working example are provided.



1. PartialDeep

What it does: Makes all properties of an object (including nested ones) optional. Recursively traverses all levels.

Parameters: One required — the object type.

import type { PartialDeep } from 'type-fest';

interface Config { server: { host: string; port: number; }; debug: boolean;}

type PartialConfig = PartialDeep<Config>;// {// server?: {// host?: string;// port?: number;// };// debug?: boolean;// }

const config: PartialConfig = { server: { host: 'localhost' // port not specified — no error }};


2. RequiredDeep

What it does: Makes all properties of an object (including nested ones) required. The opposite of PartialDeep.

Parameters: The object type.

import type { RequiredDeep } from 'type-fest';

interface User { name?: string; address?: { city?: string; };}

type StrictUser = RequiredDeep<User>;// {// name: string;// address

Recommendations