All essential TypeScript types in one place
npm install ts-essentials

All essential TypeScript types in one place 🤙
``sh`
npm install --save-dev ts-essentials
👉 We require typescript>=4.5. If you're looking for support for older TS versions, please have a look at the
TypeScript dependency table
👉 As we really want types to be stricter, we require enabled
strictNullChecks in your project
ts-essentials is a set of high-quality, useful TypeScript types that make writing type-safe code easier.
- Builtin - Matches primitive, function, date, error or regular expression
- KeyofBase -
keyofStringsOnly-tolerant analogue for PropertyKeyPrettify
- - flattens type and makes it more readable on the hover in your IDEPrimitive
- - Matches anyStrictExclude
primitive value
- - Constructs a type by excluding from UnionTypeExcludedMembers
all union members that are assignable to . This is stricter version ofExclude
StrictExtract
- - Constructs a type by extracting from Type all union membersUnion
that are assignable to . This is stricter version ofExtract
StrictOmit
- - Constructs a type by picking all properties from Type and thenKeys
removing . This is stricter version ofOmit
Writable
- - Constructs a type with removed readonly for all properties of Type, meaning
the properties of the constructed type can be reassigned
- AsyncOrSync or PromiseLikeAsyncOrSyncType
- - Unwraps AsyncOrSync typeDictionary
- - Constructs a required object type which property keys are Keysstring
( by default) and which property values are TypeMerge
- - Constructs a type by picking all properties from Object1 and Object2.Object2
Property values from override property values from Object1 when property keys are the sameMergeN
- - Constructs a type by merging objects with type Merge in tuple Tuple recursivelyNewable
- - Constructs a class type with constructor which has return type ReturnTypeNonNever
- - Constructs a type by picking all properties from type Type which values don'tnever
equal to OmitProperties
- - Constructs a type by picking all properties from type TypeValue
and removing those properties which values equal to Opaque
- - Constructs a type which is a subset of Type with a specified unique tokenToken
PathValue
- - Constructs a path value for type Type and path PathPaths
- - Constructs a union type by picking all possible paths for type TypePickProperties
- - Constructs a type by picking all properties from type TypeValue
which values equal to SafeDictionary
- - Constructs an optional object type which property keys areKeys
(string by default) and which property values are TypeUnionToIntersection
- - Constructs a intersection type from union type UnionValueOf
- - Constructs a type for type Type and equals to a primitive for primitives, arrayXOR
elements for arrays, function return type for functions or object property values for objects
- - Construct a type which is assignable to either type Type1,Type2
but not both. Starting in ts-essentials@10, it supports up to 50 generic types.
- MarkOptional whereKeys
properties are set as optional, meaning they aren't requiredMarkReadonly
- - Constructs a type by picking all properties from type Type whereKeys
properties are set to readonly, meaning they cannot be reassignedMarkRequired
- - Constructs a type by picking all properties from type Type whereKeys
properties are set as requiredMarkWritable
- - Constructs a type by picking all properties from type Type whereKeys
properties remove readonly modifier, meaning they can be reassigned
- Buildable and DeepWritable, meaning allType
properties from type are recursively set as non-readonly and optional, meaning they can be reassigned andDeepMarkOptional
aren't required
- - Constructs a type by picking all properties from type Type whereKeyPathUnion
properties by paths are set as optional. To mark properties optional on one level, use MarkOptional. DeepMarkRequired
- - Constructs a type by picking all properties from type Type whereKeyPathUnion
properties by paths are set as required. To mark properties required on one level, use MarkRequired.DeepNonNullable
- - Constructs a type by picking all properties from type Typenull
recursively and exclude and undefined property values from all of them. To make properties non-nullable onNonNullable
one level, use DeepNullable
- - Constructs a type by picking all properties from type Type recursivelynull
and include property values for all of themDeepOmit
- - Constructs a type by picking all properties from type Type and removingnever
properties which values are or true in type Filter. If you'd like type Filter to be validated against aType
structure of , please use StrictDeepOmit.DeepPartial
- - Constructs a type by picking all properties from type Type recursivelyPartial
and setting them as optional, meaning they aren't required. To make properties optional on one level, use
DeepPick
- - Constructs a type by picking set of properties, which have propertynever
values or true in type Filter, from type Type. If you'd like type Filter to be validated against aType
structure of , please use StrictDeepPick.DeepReadonly
- - Constructs a type by picking all properties from type Type recursivelyreadonly
and setting modifier, meaning they cannot be reassigned. To make properties readonly on one level, useReadonly
DeepRequired
- - Constructs a type by picking all properties from type Type recursivelyRequired
and setting as required. To make properties required on one level, use
DeepUndefinable
- - Constructs a type by picking all properties from type Typeundefined
recursively and include property values for all of themDeepWritable
- - Constructs a type by picking all properties from type Type recursivelyreadonly
and removing modifier, meaning they can be reassigned. To make properties writable on one level, useWritable
StrictDeepOmit
- - Constructs a type by picking all properties from type Typenever
and removing properties which values are or true in type Filter. The type Filter is validated against aType
structure of .StrictDeepPick
- - Constructs a type by picking set of properties, which havenever
property values or true in type Filter, from type Type. The type Filter is validated against aType
structure of .
- OptionalKeys
TypePickKeys
- - Constructs a union type by picking all properties of object type TypeValue
which values are assignable to type ReadonlyKeys
- - Constructs a union type by picking all readonly properties of objectType
type , meaning their values cannot be reassignedRequiredKeys
- - Constructs a union type by picking all required properties of object typeType
WritableKeys
- - Constructs a union type by picking all writable properties of object typeType
, meaning their values can be reassigned
- Exact when type Type and Shape are identical. Otherwise returnsnever
IsAny
- - Returns true when type Type is any. Otherwise returns falseIsNever
- - Returns true when type Type is never. Otherwise returns falseIsUnknown
- - Returns true when type Type is unknown. Otherwise returns falseIsTuple
- - Returns Type when type Type is tuple. Otherwise returns neverNonEmptyObject
-
- AnyArray or ReadonlyArray (Type is any by default)ArrayOrSingle
- - Matches Type or Type[]ElementOf
- - Constructs a type which equals to array element type for type TypeHead
- - Constructs a type which equals to first element in type TypeNonEmptyArray
- - Matches array with at least one element of type TypeReadonlyArrayOrSingle
- - Matches Type or readonly Type[]Tail
- - Constructs a type which equals to elements but first one in type TypeTuple
- - Matches type constraint for tuple with elements of type Type (any by default)
- CamelCase to camel case (e.g. camelCase)DeepCamelCaseProperties
- - Constructs a type by picking all properties fromType
type recursively and converting all of them to camel case
- AnyFunction (any[] byReturnType
default) and return type (any by default)PredicateFunction
- - Matches type constraint for type guard, meaning first argument isPredicateType
used in return type and return type is
type predicate
- - Constructs a type which equals to narrowed type in predicate functionType
⚠️ Make sure you add ts-essentials to your dependencies (npm install --save ts-essentials) to avoid runtime errors
- new UnreachableCaseError(value) - Matches runtime class instance type that
helps check exhaustiveness for value. When value isn't never, it shows TypeScript errorassert(condition, message?)
- - Matches runtime function that helps assert condition. Whencondition
is falsy, it throws an error with Assertion Error: ${message} (message is"no additional info provided"
by default)createFactoryWithConstraint
- - Matches runtimevalue
function, which validates that type of matches Constraint without changing resulting type of value.satisfies
Ponyfill for
operatorisExact
- - Matches runtime function, which validates that type ofactual
equals to Expected. Otherwise shows TypeScript errornoop(..._args)
- - Matches runtime function that does nothing with arguments _args
When one of utility types is known by a different name, kindly ask adding it here for the better search.
- ArrayValues - ValueOfBranded
- - OpaqueConditionalKeys
- - PickKeysExcept
- - StrictOmitGet
- - PathValueMutable
- - WritableNominal
- - OpaqueSet
- , e.g. SetOptional - Mark, e.g. MarkReadonlyUnwrap
- - PrettifyValueOf
- - DictionaryValues
TypeScript provides several utility types to
facilitate common type transformations. These utilities are available globally.
- Awaited
model operations like await in async functions, or the .then() method on Promises - specifically, the way thatPromise
they recursively unwrap sCapitalize
- -ConstructParameters
Converts the first character in the string to an uppercase equivalent
- -Type
Constructs a tuple or array type from the types of a constructor function type Exclude
- -UnionType
Constructs a type by excluding from all union members that are assignable to ExcludedMembersExtract
- -Type
Constructs a type by extracting from all union members that are assignable to UnionInstanceType
- - ConstructsType
a type consisting of the instance type of a constructor function in Lowercase
- -NonNullable
Converts each character in the string to the lowercase equivalent
- - Constructs aType
type by excluding null and undefined from Omit
- - Constructs aType
type by picking all properties from and then removing KeysParameters
- - Constructs aType
tuple type from the types used in the parameters of a function type Partial
- - Constructs a typeType
with all properties of set to optionalPick
- - Constructs aKeys
type by picking the set of properties from TypeReadonly
- - Constructs a typeType
with all properties of set to readonly, meaning the properties of the constructed type cannot be reassignedRecord
- - ConstructsKeys
an object type whose property keys are and whose property values are TypeRequired
- - Constructs a typeType
consisting of all properties of set to requiredReturnType
- - Constructs aType
type consisting of the return type of function type parameterUncapitalize
- -Uppercase
Converts the first character in the string to a lowercase equivalent
- -
Converts each character in the string to the uppercase version
| ts-essentials | typescript / type of dependency |^10.0.0
| --------------- | ------------------------------------------------------------------------------------- |
| | ^4.5.0 / peer optional |^9.4.0
| | ^4.1.0 / peer optional |^8.0.0
| | ^4.1.0 / peer |^5.0.0
| | ^3.7.0 / peer |^3.0.1
| | ^3.5.0 / peer |^1.0.1
| | ^3.2.2 / dev |^1.0.0
| | ^3.0.3 / dev |
- This project doesn't use extends Constraints on infer` Type Variables as it's introduced in TypeScript 4.7, but currently ts-essentials supports versions below, e.g. TypeScript 4.5. Read more in https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#extends-constraints-on-infer-type-variables
Special shout-out to active contributors:
- Kris Kaczor / 💻
- Xiao Liang / 💻
- Mateusz Burzyński / 💻
- Artur Kozak / 💻
- Zihua Wu / 💻
- Alexey Berezin / 💻
- Som Shekhar Mukherjee / 💻
And thanks goes to these wonderful people:
- Maciej Bembenista / 💻
- Michael Tontchev / 💻
- Thomas den Hollander / 💻
- Esa-Matti Suuronen
- Ilya Semenov / 💻
- Patricio Palladino
- Kevin Peno / 💻
- Dom Parfitt
- Eduardo Rafael / 💻
- Andrew C. Dvorak / 💻
- Adam Russell / 💻
- Piotr Szlachciak / 💻
- Mikhail Swift / 💻
- Ryan Zhang
- Francesco Borzì / 💻
- Marnick L'Eau
- Egor Gorbachev
- Bill Barry / 💻
- Andrzej Wódkiewicz / 💻
- Christian Junker
- Matthew Leffler / 💻
- studds / 💻
- Robert Vitonsky
- Itay Ronen / 💻
- Yaroslav Larin
- liaoyinglong / 💻
- Yannick Stachelscheid / 💻
- Peter Smolinský / 💻
- Anurag Hazra / 💻
- Bishwajit Jha / 💻
- Sergey Ukustov / 💻
- Homa Wong / 💻
- polyipseity / 💻
- Kristóf Poduszló / 💻
- MT Lewis / 💻
- Daniel Bertocci / 💻
- Myles J / 💻
💻 - contributions, i.e. links to commits by the user on this project
Contributions of any kind welcome! Read more