JAEGIS-METHOD-v1.0\node_modules\type-fest\readme
A collection of essential TypeScript types
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-festRequires TypeScript >=3.4
Usage
import {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
Primitive- Matches any primitive value.Class- Matches aclassconstructor.TypedArray- Matches any typed array, likeUint8ArrayorFloat64Array.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
Mutable- Convert an object withreadonlykeys into a mutable object. The inverse ofReadonly<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 keys.RequireAtLeastOne- Create a type that requires at least one of the given keys.RequireExactlyOne- Create a type that requires exactly a single key of the given keys and disallows more.PartialDeep- Create a deeply optional version of another type. UsePartial<T>if you only need one level deep.ReadonlyDeep- Create a deeply immutable version of anobject/Map/Set/Arraytype. UseReadonly<T>if you only need one level deep.LiteralUnion- Create 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.Promisable- Create a type that represents either the value or the value wrapped inPromiseLike.Opaque- Create an opaque type.SetOptional- Create a type that makes the given keys optional.SetRequired- Create a type that makes the given keys required.ValueOf- Create a union of the given object's values, and optionally specify which keys to get the values from.PromiseValue- Returns the type that is wrapped inside aPromise.AsyncReturnType- Unwrap the return type of a function that returns aPromise.ConditionalKeys- Extract keys from a shape where values extend the givenConditiontype.ConditionalPick- LikePickexcept it selects properties from a shape where the values extend the givenConditiontype.ConditionalExcept- LikeOmitexcept it removes properties from a shape where the values extend the givenConditiontype.UnionToIntersection- Convert a union type to an intersection type.Stringified- Create a type with the keys of the given type changed tostringtype.FixedLengthArray- Create a type that represents an array of the given type and length.IterableElement- Get the element type of anIterable/AsyncIterable. For example, an array or a generator.Entry- Create a type that represents the type of an entry of a collection.Entries- Create a type that represents the type of the entries of a collection.SetReturnType- Create a function type with a return type of your choice and the same parameters as the given function type.Asyncify- Create an async version of the given function type.
Template literal types
Note: These require TypeScript 4.1 or newer.
CamelCaseโ Convert a string literal to camel-case (fooBar).KebabCaseโ Convert a string literal to kebab-case (foo-bar).PascalCaseโ Converts a string literal to pascal-case (FooBar)SnakeCaseโ Convert a string literal to snake-case (foo_bar).DelimiterCaseโ Convert a string literal to a custom string delimiter casing.
Miscellaneous
PackageJson- Type for npm'spackage.jsonfile.TsConfigJson- Type for TypeScript'stsconfig.jsonfile (TypeScript 3.7).
Declined types
If we decline a type addition, we will make sure to document the better solution here.
DiffandSpread- The PR author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.Dictionary- You only save a few characters (Dictionary<number>vsRecord<string, number>) fromRecord, which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We haveMapin JavaScript now.SubType- The type is powerful, but lacks good use-cases and is prone to misuse.ExtractPropertiesandExtractMethods- The types violate the single responsibility principle. Instead, refine your types into more granular type hierarchies.
Tips
Built-in types
There are many advanced types most users don't know about.
Partial<T>- Make all properties inToptional.Required<T>- Make all properties inTrequired.Readonly<T>- Make all properties inTreadonly.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 propertiesKof typeT.Exclude<T, U>- Exclude fromTthose types that are assignable toU.Extract<T, U>- Extract fromTthose types that are assignable toU.NonNullable<T>- ExcludenullandundefinedfromT.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.Omit<T, K>โ Constructs a type by picking all properties from T and then removing K.
You can find some examples in the TypeScript docs.
Maintainers
License
(MIT OR CC0-1.0)
Get professional support for this package with a Tidelift subscription Tidelift helps make open source sustainable for maintainers while giving companies assurances about security, maintenance, and licensing for their dependencies.
Last updated