type-fest README

The project's README from github.com/sindresorhus/type-fest. As one markdown file: README.md.



type-fest

A collection of essential TypeScript types





npm dependents npm downloads

Many of the types here should have been built-in. You can help by suggesting some of them to the TypeScript project.

Either add this package as a dependency or copy-paste the needed types. No credit required. 👌

PR welcome for additional commonly needed types and docs improvements. Read the contributing guidelines first.

Help wanted with reviewing proposals and pull requests.

Install

npm install type-fest

Requires TypeScript >=6, ESM, and {strict: true} in your tsconfig.

{exactOptionalPropertyTypes: true} is required. Disabling it is unsupported.

[!NOTE] This readme shows the current development version. For docs about the latest version, see the npm page.

You may also like my ts-extras package which provides runtime functions for some of these types.

Usage

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

type Foo = {
	unicorn: string;
	rainbow: boolean;
};

type FooWithoutRainbow = Except<Foo, 'rainbow'>;
//=> {unicorn: string}

Try it in the TypeScript Playground

API

Click the type names for complete docs.

Basic

Utilities

Type Guard

JSON

Structured clone

Function

Async

String

Array

Numeric

Change case

Miscellaneous

Improved built-in

Declined types

If we decline a type addition, we will make sure to document the better solution here.

Alternative type names

If you know one of our types by a different name, add it here for discovery.

Tips

Extending existing types

“Cannot be named” errors in declaration files

When TypeScript generates declaration files, it writes out the inferred type of each export. If that type uses a type from this package, it can refer to an internal helper type or symbol, and you get an error such as TS2742, TS2883, TS4023, or TS4058 (“The inferred type of 'x' cannot be named without a reference to…”). Internal types and symbols are not part of the public API, so they cannot be imported. This is a TypeScript limitation. Add an explicit type annotation to the export instead:

import type {EmptyObject, ReadonlyDeep} from 'type-fest';

type Config = {
	server: {
		port: number;
	};
};

declare function freeze<Value>(value: Value): ReadonlyDeep<Value>;

export const config: ReadonlyDeep<Config> = freeze({server: {port: 8080}});

export function copy(value: EmptyObject): EmptyObject {
	return {...value};
}

Also make sure type-fest is a direct dependency of the package that generates the declaration files.

Built-in types

There are many advanced types most users don't know about.

You can find some examples in the TypeScript docs.

Maintainers

License

SPDX-License-Identifier: (MIT OR CC0-1.0)

type-fest (sindresorhus/type-fest)

Searching Database…

What is this?type-fest is a collection of essential TypeScript types — helper types the author feels should have been built into the language, covering objects, unions, strings, arrays, tuples and functions.
Is it a runtime library or just type definitions?Just types — definitions your compiler consumes at build time; nothing executes in your app. For runtime functions the author publishes a separate companion package, ts-extras.
How do I add it to a project?Install it as a dependency with npm install type-fest and import the types you need — or skip the package entirely and copy-paste just the definitions you want.
Does my project need a specific TypeScript setup?Yes — TypeScript 5.9 or newer, ESM, and strict mode ({strict: true}) in tsconfig; older or non-strict codebases will not accept it. Also, the GitHub README documents the unreleased dev version, so released docs live on npm.
What kinds of types does it include?Well over a hundred, in three groups: Basic (Primitive, Class, TypedArray), Utilities (Except, Merge, LiteralUnion, PartialDeep, Get...) and Type Guards (If, IsLiteral, IsNumericLiteral).
Which types are worth knowing first?Except removes keys from an object type, Merge combines two types, LiteralUnion keeps IDE autocomplete for literal unions, and Get fetches a nested property like Lodash's .get().
Do I have to credit the author if I copy-paste types into my code?No — type-fest is licensed CC0-1.0, effectively public domain, and the README says copy-pasting the needed types requires no credit.
Does it keep up with new TypeScript releases?Likely yes: the repo's CI includes a canary workflow that tests unreleased TypeScript builds, and the README encourages suggesting types that belong in the language to the TypeScript project itself.
Size
5.3 MB
Stars
17.5K
Age
7y
Commits · 90 Days