Type-Fest
Type-Fest is a comprehensive collection of essential TypeScript utility types designed to simplify complex type manipulations and enhance type safety in modern TypeScript projects. Currently stable at version 5.6.0, the library maintains an active release cadence, frequently introducing new types and improving existing ones with minor and patch updates. It serves as a valuable resource for developers seeking robust, well-tested types that address common TypeScript patterns, often providing functionalities that many believe should be part of TypeScript's standard library. Key differentiators include its focus on practical, real-world utility types, its explicit requirement for TypeScript >=5.9 and ESM (ECMAScript Modules) environments, and the recommendation to enable `strict: true` in `tsconfig.json` for optimal usage. The library is also noteworthy for allowing developers to copy and paste types directly into their projects without attribution, promoting broad adoption.
Common errors
-
SyntaxError: Named export 'Except' not found. The requested module 'type-fest' does not provide an export named 'Except'
cause Attempting to import `type-fest` types in a CommonJS environment, or using an incorrect import syntax for ESM.fixEnsure your project is configured for ESM and use `import type { Except } from 'type-fest';`. If using `require()`, switch to ESM `import` statements or use `type-fest@4` or earlier. -
Type 'string | undefined' is not assignable to type 'string'.
cause Your `tsconfig.json` does not have `"strict": true` enabled, leading to less precise type inference, especially when working with optional properties or null/undefined values, which `type-fest` often helps manage in a strict way.fixAdd `"strict": true` to your `compilerOptions` in `tsconfig.json`. Review and fix any new type errors that appear, as they usually indicate genuine potential runtime issues. -
error TS2307: Cannot find module 'type-fest' or its corresponding type declarations.
cause TypeScript version is too old, the package is not installed correctly, or module resolution issues.fixEnsure `npm install type-fest` has been run. Verify your TypeScript version is `>=5.9`. Check your `tsconfig.json`'s `moduleResolution` and `module` settings are compatible with ESM and modern TypeScript best practices.
Warnings
- breaking Type-Fest versions 4.x and earlier supported CommonJS (CJS) modules. Starting with version 5.0.0, the library is exclusively ESM-first and no longer provides CJS exports. Attempting to `require()` Type-Fest will result in a runtime error.
- breaking Type-Fest requires TypeScript version 5.9 or higher. Using an older TypeScript version will lead to compilation errors due to reliance on newer TypeScript features and syntax.
- gotcha Many Type-Fest types are designed to work optimally and correctly infer types only when `strict: true` is enabled in your `tsconfig.json`. Without strict mode, some types may behave unexpectedly or provide less precise results.
- gotcha When importing types from `type-fest`, always use the `import type` syntax (`import type { TypeName } from 'type-fest';`). While not strictly an error in all cases, using `import { TypeName } from 'type-fest';` (without `type`) can sometimes lead to runtime import errors in certain build environments or with specific bundlers, as `type-fest` only exports types, not runnable JavaScript values.
Install
-
npm install type-fest -
yarn add type-fest -
pnpm add type-fest
Imports
- Except
import {Except} from 'type-fest';import type {Except} from 'type-fest'; - Merge
const {Merge} = require('type-fest');import type {Merge} from 'type-fest'; - PackageJson
import {PackageJson} from 'type-fest/source/package-json';import type {PackageJson} from 'type-fest';
Quickstart
import type {Except, Merge, Simplify} from 'type-fest';
interface User {
id: string;
name: string;
email: string;
createdAt: Date;
updatedAt?: Date;
}
interface UserProfile {
bio: string;
website?: string;
}
// Create a new user type that excludes 'id' and 'createdAt'
type NewUser = Except<User, 'id' | 'createdAt'>;
// Merge the new user type with a profile to create a complete user
type FullUser = Simplify<Merge<NewUser, UserProfile>>;
const newUser: FullUser = {
name: 'Alice Wonderland',
email: 'alice@example.com',
bio: 'Exploring type safety in Wonderland.',
website: 'https://alice.example.com'
};
console.log(newUser);
// Demonstrating a common pattern with strict mode
// Type-fest types are designed with strict mode in mind.
// For example, if 'updatedAt' was not optional and not provided,
// TypeScript would catch the missing property if strict: true is enabled.
const partialUserUpdate: Partial<NewUser> = { name: 'Alice Smith' };
console.log(partialUserUpdate);