9.4.1 β€’ Published 6 months ago

ts-essentials v9.4.1

Weekly downloads
1,856,495
License
MIT
Repository
github
Last release
6 months ago

Install

npm install --save-dev ts-essentials

πŸ‘‰ We require typescript>=4.1. 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

API

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

Basic

Utility types

  • AsyncOrSync<Type> - Constructs a type with Type or PromiseLike<Type>
  • AsyncOrSyncType<Type> - Unwraps AsyncOrSync type
  • Dictionary<Type, Keys?> - Constructs a required object type which property keys are Keys (string by default) and which property values are Type
  • DictionaryValues<Type> - This type unwraps Dictionary value type
  • Merge<Object1, Object2> - 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> - Constructs a type by merging objects with type Merge in tuple Tuple recursively
  • Newable<ReturnType> - Constructs a class type with constructor which has return type ReturnType
  • NonNever<Type> - Constructs a type by picking all properties from type Type which values don't equal to never
  • OmitProperties<Type, Value> - Constructs a type by picking all properties from type Type and removing those properties which values equal to Value
  • Opaque<Type, Token> - Constructs a type which is a subset of Type with a specified unique token Token
  • PickProperties<Type, Value> - Constructs a type by picking all properties from type Type which values equal to Value
  • SafeDictionary<Type, Keys?> - Constructs an optional object type which property keys are Keys (string by default) and which property values are Type
  • UnionToIntersection<Union> - Constructs a intersection type from union type Union
  • ValueOf<Type> - 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> - Construct a type which is assignable to either type Type1 or Type2 but not both

Mark wrapper types

  • MarkOptional<Type, Keys> - 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> - 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> - Constructs a type by picking all properties from type Type where properties Keys are set as required
  • MarkWritable<Type, Keys> - 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> - 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> - 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>
  • DeepNullable<Type> - Constructs a type by picking all properties from type Type recursively and include null property values for all of them
  • DeepOmit<Type, Filter> - Constructs a type by picking all properties from type Type and removing properties which values are never or true in type Filter
  • DeepPartial<Type> - 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>
  • DeepPick<Type, Filter> - Constructs a type by picking set of properties, which have property values never or true in type Filter, from type Type
  • DeepReadonly<Type> - 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>
  • DeepRequired<Type> - 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>
  • DeepUndefinable<Type> - Constructs a type by picking all properties from type Type recursively and include undefined property values for all of them
  • DeepWritable<Type> - 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>

Key types

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

Type checkers

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

Arrays and Tuples

Change case

Function types

Utility functions

⚠️ Make sure you add ts-essentials to your dependencies (npm install --save ts-essentials) to avoid runtime errors

Built-in types

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

  • Awaited<Type> - This type is meant to model operations like await in async functions, or the .then() method on Promises - specifically, the way that they recursively unwrap Promises
  • Capitalize<StringType> - Converts the first character in the string to an uppercase equivalent
  • ConstructParameters<Type> - Constructs a tuple or array type from the types of a constructor function type Type
  • Exclude<UnionType, ExcludedMembers> - Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers
  • Extract<Type, Union> - Constructs a type by extracting from Type all union members that are assignable to Union
  • InstanceType<Type> - Constructs a type consisting of the instance type of a constructor function in Type
  • Lowercase<StringType> - Converts each character in the string to the lowercase equivalent
  • NonNullable<Type> - Constructs a type by excluding null and undefined from Type
  • Omit<Type, Keys> - Constructs a type by picking all properties from Type and then removing Keys
  • Parameters<Type> - Constructs a tuple type from the types used in the parameters of a function type Type
  • Partial<Type> - Constructs a type with all properties of Type set to optional
  • Pick<Type, Keys> - Constructs a type by picking the set of properties Keys from Type
  • Readonly<Type> - Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned
  • Record<Keys, Type> - Constructs an object type whose property keys are Keys and whose property values are Type
  • Required<Type> - Constructs a type consisting of all properties of Type set to required
  • ReturnType<Type> - Constructs a type consisting of the return type of function type Type parameter
  • Uncapitalize<StringType> - Converts the first character in the string to a lowercase equivalent
  • Uppercase<StringType> - Converts each character in the string to the uppercase version

TypeScript dependency table

ts-essentialstypescript / type of dependency
^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

Contributors

Thanks goes to these wonderful people (emoji key):

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

@loopx/log@atomist/sdm@atomist/sdm-core@borie88/lucid@cerberustech/scylla-typingsidentity-ws@codeloop/authentication-josa@codeloop/authentication-jotbinpackr@remly/channel@remly/schedule@remly/server@remly/tcp@remly/tcp-client@remly/testlab@remly/testsuite@remly/transport-tests@remly/wsnestjs-jester@provair/cli@kikko-land/core@triply/client.jsfaros-cli@infinitebrahmanuniverse/nolb-ts-e@everything-registry/sub-chunk-2976laborealiquidleylinesjsknubjest-unitjest-class-extendedjest-mock-extendedmobx-react-typed-injectionmpesa-cookiempesa-cookie-jarloopback4-querynucluimasmottloose-rgbloopback4-testlabflow-kafka-pipelinesflow-rdfgrammy-scenesgraphql-ts-clienthardhat-teaplughardhat-teaxyzfaros-js-clientfdappfht-cms-settings-sharedethers-typed-contractethers-v6-resultethereum-sources-downloadereve-commonfuelchainhaomaoaf-react-final-formpulse-typechainpulse-typechain-v5ppcn-metricsppppopmotion-pppposepopmotion-posereact-final-form-plusreact-c4react-hotel-calendarreact-hotel-datepickerreact-lite-formozkar-tplozkar-zerkleparaswapparaswap-sdkreact-streamypayloadddjohnperimeterx-js-coreperspiciatisistepayload-custompayload-johnplausible-tracker-nodereact-designer-componentpromster-ppcn-metricsrepudiandaeeiussequelcomponent@elwood/types@elwood-studio/types@ehacke/pubsub@haggholm/json-schema-merge-allof@happyfarmstudios/magento-sdk@eth-dx/sdk-cli@grund/auth-utils@grund/tools@getbalance/automock-jest@emuanalytics/flow-rdf@frontity/types@fortejs/forte@frictionlessdata/application@flarezone/connect-kit@hovoh/typechain-ethers-multicall@hovoh/typechain-ethers-v5@ikomiki/nx-aws-sam@imaginary-dev/babel-transformer@front.zen/data-service@fullerstack/nax-ipware
9.4.1

6 months ago

9.4.0

7 months ago

9.3.2

11 months ago

9.3.1

1 year ago

9.3.0

2 years ago

9.2.0

2 years ago

9.1.1

2 years ago

9.1.0

2 years ago

9.1.2

2 years ago

9.0.0

2 years ago

8.1.0

3 years ago

8.0.0

3 years ago

7.0.3

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

7.0.0

4 years ago

6.0.7

4 years ago

6.0.6

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.0.0

4 years ago

4.0.0

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.12

5 years ago

3.0.0-beta.1

5 years ago

3.0.0-beta.0

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago