What it is
type-fest is a collection of essential TypeScript utility types that the standard library does not include, from deep partials to JSON value types.
Import only the types you need. Everything is types-only, so nothing is emitted into your bundle.
Installation
npm install --save-dev type-festGetting started
The smallest useful thing you can do with it, and what each part means.
typescript
import type {
PartialDeep, ReadonlyDeep, RequireAtLeastOne,
SetOptional, Jsonify, JsonValue,
} from 'type-fest';
interface Config {
server: { host: string; port: number };
logging: { level: string };
}
// Built-in Partial is shallow; this one recurses.
type Overrides = PartialDeep<Config>;
// Force callers to supply at least one identifier.
type Lookup = RequireAtLeastOne<{ id?: number; slug?: string }, 'id' | 'slug'>;
// Make specific fields optional rather than all of them.
type Draft = SetOptional<Book, 'id' | 'createdAt'>;Advanced usage
Where the library earns its place over a simpler alternative.
typescript
import type { Jsonify } from 'type-fest';
interface Book {
id: number;
title: string;
published: Date; // becomes a string over the wire
tags: Set<string>; // does not survive JSON at all
}
// What the client actually receives.
type ApiBook = Jsonify<Book>;
// { id: number; title: string; published: string }
function render(book: ApiBook) {
new Date(book.published); // correct: it is a string here
}Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Type instantiation is excessively deep
- A deep utility hit a recursive structure. Simplify the type or apply the utility to a smaller slice.
- The utility does nothing
- Several require a second type parameter naming the keys — check the signature; RequireAtLeastOne and SetOptional both do.
Best practices
- Use `import type` so nothing reaches the runtime bundle.
- Reach for type-fest before writing a complex conditional type by hand.
- Use Jsonify for API response types instead of reusing the server model.
- Keep it in devDependencies — it is types only.
Background
Why it exists, and what it was reacting to.
Maintained by Sindre Sorhus, type-fest collects the advanced types that developers otherwise rewrite in every project — often subtly wrong.
