ts-essentials

All basic TypeScript types in one place

README

ts-essentials


Install


  1. ```sh
  2. npm install --save-dev ts-essentials
  3. ```

👉 We require typescript&rt;=4.1. If you're looking for support for older TS versions, please have a look at the

👉 As we really want types to be stricter, we require enabled
strictNullChecks in your project

API


ts-essentials is a set of high-quality, useful TypeScript types that make writing type-safe code easier.

Basic


- Builtin - Matches primitive, function, date, error or regular expression
- KeyofBase -
  keyofStringsOnly -tolerant analogue for PropertyKey
- Primitive  - Matches any
- StrictExclude<UnionType, ExcludedMembers&rt;  - Constructs a type by excluding from UnionType
  all union members that are assignable to ExcludedMembers. This is stricter version of
  Exclude
- StrictExtract<Type, Union&rt; - Constructs a type by extracting from Type all union members
  that are assignable to Union. This is stricter version of
  Extract
- StrictOmit<Type, Keys&rt; - Constructs a type by picking all properties from Type and then
  removing Keys. This is stricter version of
  Omit
- Writable<Type&rt; - Constructs a type with removed readonly for all properties of Type, meaning
  the properties of the constructed type can be reassigned

Utility types


- AsyncOrSync<Type&rt;  - Constructs a type with Type or PromiseLike<Type&rt;
- AsyncOrSyncType<Type&rt;  - Unwraps AsyncOrSync type
- Dictionary<Type, Keys?&rt;  - Constructs a required object type which property keys are Keys
  (string by default) and which property values are Type
- DictionaryValues<Type&rt;  - This type unwraps Dictionary value type
- Merge<Object1, Object2&rt;  - Constructs a type by picking all properties from Object1 and Object2.
  Property values from Object2 override property values from Object1 when property keys are the same
- MergeN<Tuple&rt;  - Constructs a type by merging objects with type Merge in tuple Tuple recursively
- Newable<ReturnType&rt;  - Constructs a class type with constructor which has return type ReturnType
- NonNever<Type&rt;  - Constructs a type by picking all properties from type Type which values don't
  equal to never
- OmitProperties<Type, Value&rt;  - Constructs a type by picking all properties from type Type
  and removing those properties which values equal to Value
- Opaque<Type, Token&rt;  - Constructs a type which is a subset of Type with a specified unique token
  Token
- PickProperties<Type, Value&rt;  - Constructs a type by picking all properties from type Type
  which values equal to Value
- SafeDictionary<Type, Keys?&rt;  - Constructs an optional object type which property keys are
  Keys (string by default) and which property values are Type
- UnionToIntersection<Union&rt;  - Constructs a intersection type from union type Union
- ValueOf<Type&rt;  - Constructs a type for type Type and equals to a primitive for primitives, array
  elements for arrays, function return type for functions or object property values for objects
- XOR<Type1, Type2&rt;  - Construct a type which is assignable to either type Type1 or Type2 but not both

Mark wrapper types


- MarkOptional<Type, Keys&rt;  - Constructs a type by picking all properties from type Type where
  properties Keys are set as optional, meaning they aren't required
- MarkReadonly<Type, Keys&rt;  - Constructs a type by picking all properties from type Type where
  properties Keys are set to readonly, meaning they cannot be reassigned
- MarkRequired<Type, Keys&rt;  - Constructs a type by picking all properties from type Type where
  properties Keys are set as required
- MarkWritable<Type, Keys&rt;  - Constructs a type by picking all properties from type Type where
  properties Keys remove readonly modifier, meaning they can be reassigned

Deep wrapper types


- Buildable<Type&rt;  - Constructs a type by combining DeepPartial and DeepWritable, meaning all
  properties from type Type are recursively set as non-readonly and optional, meaning they can be reassigned and
  aren't required
- DeepNonNullable<Type&rt;  - Constructs a type by picking all properties from type Type
  recursively and exclude null and undefined property values from all of them. To make properties non-nullable on
  one level, use NonNullable<Type&rt;
- DeepNullable<Type&rt;  - Constructs a type by picking all properties from type Type recursively
  and include null property values for all of them
- DeepOmit<Type, Filter&rt;  - Constructs a type by picking all properties from type Type and removing
  properties which values are never or true in type Filter
- DeepPartial<Type&rt;  - Constructs a type by picking all properties from type Type recursively
  and setting them as optional, meaning they aren't required. To make properties optional on one level, use
  Partial<Type&rt;
- DeepPick<Type, Filter&rt;  - Constructs a type by picking set of properties, which have property
  values never or true in type Filter, from type Type
- DeepReadonly<Type&rt;  - Constructs a type by picking all properties from type Type recursively
  and setting readonly modifier, meaning they cannot be reassigned. To make properties readonly on one level, use
  Readonly<Type&rt;
- DeepRequired<Type&rt;  - Constructs a type by picking all properties from type Type recursively
  and setting as required. To make properties required on one level, use
  Required<Type&rt;
- DeepUndefinable<Type&rt;  - Constructs a type by picking all properties from type Type
  recursively and include undefined property values for all of them
- DeepWritable<Type&rt;  - Constructs a type by picking all properties from type Type recursively
  and removing readonly modifier, meaning they can be reassigned. To make properties writable on one level, use
  Writable<Type&rt;

Key types


- OptionalKeys<Type&rt;  - Constructs a union type by picking all optional properties of object type
  Type
- PickKeys<Type, Value&rt;  - Constructs a union type by picking all properties of object type Type
  which values are assignable to type Value
- ReadonlyKeys<Type&rt;  - Constructs a union type by picking all readonly properties of object
  type Type, meaning their values cannot be reassigned
- RequiredKeys<Type&rt;  - Constructs a union type by picking all required properties of object type
  Type
- WritableKeys<Type&rt;  - Constructs a union type by picking all writable properties of object type
  Type, meaning their values can be reassigned

Type checkers


- Exact<Type, Shape&rt;  - Returns Type when type Type and Shape are identical. Otherwise returns
  never
- IsAny<Type&rt;  - Returns true when type Type is any. Otherwise returns false
- IsNever<Type&rt;  - Returns true when type Type is never. Otherwise returns false
- IsUnknown<Type&rt;  - Returns true when type Type is unknown. Otherwise returns false
- IsTuple<Type&rt;  - Returns Type when type Type is tuple. Otherwise returns never
- NonEmptyObject<Object&rt;  - Returns Object when Object has at least one key. Otherwise
  returns never

Arrays and Tuples


- AnyArray<Type?&rt;  - Matches Array or ReadonlyArray (Type is any by default)
- ArrayOrSingle<Type&rt;  - Matches Type or Type[]
- ElementOf<Type&rt;  - Constructs a type which equals to array element type for type Type
- Head<Type&rt;  - Constructs a type which equals to first element in type Type
- NonEmptyArray<Type&rt;  - Matches array with at least one element of type Type
- ReadonlyArrayOrSingle  - Matches Type or readonly Type[]
- Tail<Type&rt;  - Constructs a type which equals to elements but first one in type Type
- Tuple<Type?&rt;  - Matches type constraint for tuple with elements of type Type (any by default)

Change case


- CamelCase<Type&rt;  - Converts type Type to camel case (e.g. camelCase)
- DeepCamelCaseProperties<Type&rt;  - Constructs a type by picking all properties from
  type Type recursively and converting all of them to camel case

Function types


- AnyFunction<Args?, ReturnType?&rt;  - Matches function type with arguments type Args (any[] by
  default) and return type ReturnType (any by default)
- PredicateFunction  - Matches type constraint for type guard, meaning first argument is
  used in return type and return type is
- PredicateType<Type&rt;  - Constructs a type which equals to narrowed type in predicate function
  Type

Built-in types


TypeScript provides several utility types to
facilitate common type transformations. These utilities are available globally.

Contributors


Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification.
Contributions of any kind welcome! Read more