lihai 8d6c751f49 feat: push | 2 years ago | |
---|---|---|
.. | ||
source | 2 years ago | |
index.d.ts | 2 years ago | |
license | 2 years ago | |
package.json | 2 years ago | |
readme.md | 2 years ago |
<br>
<br>
<img src="media/logo.svg" alt="type-fest" height="300">
<br>
<br>
<b>A collection of essential TypeScript types</b>
<br>
<hr>
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.
$ npm install type-fest
import {Omit} from 'type-fest';
type Foo = {
unicorn: string;
rainbow: boolean;
};
type FooWithoutRainbow = Omit<Foo, 'rainbow'>;
//=> {unicorn: string}
Click the type names for complete docs.
Primitive
- Matches any primitive value.Class
- Matches a class
constructor.TypedArray
- Matches any typed array, like Uint8Array
or Float64Array
.JsonObject
- Matches a JSON object.JsonArray
- Matches a JSON array.JsonValue
- Matches any valid JSON value.ObservableLike
- Matches a value that is like an Observable.Omit
- Create a type from an object type without certain keys.Mutable
- Convert an object with readonly
properties into a mutable object. Inverse of Readonly<T>
.Merge
- Merge two types into a new type. Keys of the second type overrides keys of the first type.MergeExclusive
- Create a type that has mutually exclusive properties.RequireAtLeastOne
- Create a type that requires at least one of the given properties.LiteralUnion
- Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for Microsoft/TypeScript#29729.PackageJson
- Type for npm's package.json
file.If we decline a type addition, we will make sure to document the better solution here.
There are many advanced types most users don't know about.
Partial<T>
- Make all properties in T
optional.Required<T>
- Make all properties in T
required.Readonly<T>
- Make all properties in T
readonly.Pick<T, K>
- From T
, pick a set of properties whose keys are in the union K
.Record<K, T>
- Construct a type with a set of properties K
of type T
.Exclude<T, U>
- Exclude from T
those types that are assignable to U
.Extract<T, U>
- Extract from T
those types that are assignable to U
.NonNullable<T>
- Exclude null
and undefined
from T
.Parameters<T>
- Obtain the parameters of a function type in a tuple.ConstructorParameters<T>
- Obtain the parameters of a constructor function type in a tuple.ReturnType<T>
– Obtain the return type of a function type.InstanceType<T>
– Obtain the instance type of a constructor function type.You can find some examples in the TypeScript docs.
(MIT OR CC0-1.0)