sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
13.64k stars 515 forks source link
npm-package types typescript typescript-definitions utilities


type-fest

A collection of essential TypeScript types




Sindre Sorhus' open source work is supported by the community

Special thanks to:




WorkOS
Your app, enterprise-ready.
Start selling to enterprise customers with just a few lines of code.
Add Single Sign-On (and more) in minutes instead of months.



Transloadit logo


Logto logo
The better identity infrastructure for developers
Logto is an open-source Auth0 alternative designed for every app.






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 >=5.1

Works best with {strict: true} in your tsconfig.

Usage

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

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

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

API

Click the type names for complete docs.

Basic

Utilities

Type Guard

IsType vs. IfType

For every IsT type (e.g. IsAny), there is an associated IfT type that can help simplify conditional types. While the IsT types return a boolean, the IfT types act like an If/Else - they resolve to the given TypeIfT or TypeIfNotT depending on whether IsX is true or not. By default, IfT returns a boolean:

type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
    IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
);

Usage

import type {IsAny, IfAny} from 'type-fest';

type ShouldBeTrue = IsAny<any> extends true ? true : false;
//=> true

type ShouldBeFalse = IfAny<'not any'>;
//=> false

type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
//=> 'never'

JSON

Structured clone

Async

String

Array

Numeric

Change case

Miscellaneous

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

Related

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)