GitHub - sindresorhus/type-fest: A collection of essential TypeScript types
source link: https://github.com/sindresorhus/type-fest
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
readme.md
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.
Install
$ npm install type-fest
Usage
import {Omit} from 'type-fest'; type Foo = { unicorn: string; rainbow: boolean; }; type FooWithoutRainbow = Omit<Foo, 'rainbow'>; //=> {unicorn: string}
API
See the types file for complete docs.
Basic
Primitive
- Matches any primitive value.Class
- Matches aclass
constructor.TypedArray
- Matches any typed array, likeUint8Array
orFloat64Array
.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.
Utilities
Omit
- Create a type from an object type without certain keys.Merge
- Merge two types into a new type. Keys of the second type overrides keys of the first type.
Declined types
If we decline a type addition, we will make sure to document the better solution here.
Tips
Built-in types
There are many advanced types most users don't know about.
Partial<T>
- Make all properties inT
optional.Required<T>
- Make all properties inT
required.Readonly<T>
- Make all properties inT
readonly.Pick<T, K>
- FromT
, pick a set of properties whose keys are in the unionK
.Record<K, T>
- Construct a type with a set of propertiesK
of typeT
.Exclude<T, U>
- Exclude fromT
those types that are assignable toU
.Extract<T, U>
- Extract fromT
those types that are assignable toU
.NonNullable<T>
- Excludenull
andundefined
fromT
.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.
License
(MIT OR CC0-1.0)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK