ts-typetools
v1.1.0
Published
A collection of TypeScript type, type expressions, guard functions, and type-smart identity functions.
Downloads
27,880
Maintainers
Readme
TypeTools are a collection of TypeScript type, type expressions and functions that aid writing idiomatic JavaScript in TypeScript a little easier.
Convenience Type Aliases
Falsy
represents the type of all falsy values.NullLike
represents thenull
andundefined
types.Key
represents the type of all valid keys, namelynumber
,string
andsymbol
.Primitive
represents the type of all primitive (not object) values, namelystring
,number
,boolean
,symbol
,null
,undefined
.Concrete
represents the type of everything that is notnull
orundefined
, (the opposite ofNullLike
).
Convenience Type Expressions
MaybePromise<T>
is a shortcut forT | Promise<T>
.ResolveType<T>
obtains theresolve()
type of a promiseT
, or theresolve()
type of the return value of a functionT
, otherwiseT
itself.MaybeArray<T>
is a shortcut forT | Array<T>
.ArrayItemType<T>
obtains the element type of an arrayT
, otherwiseT
itself.ExtractRefine<T, U, V>
refines part of typeT
that extendsU
(like the built-inExtract
type) by interesecting that extraction with theV
type.ExtractOmit<T, U, K>
selectively omits propertiesK
from a part of typeT
that extendsU
.ExtractReplace<T, U, V>
selectively replaces a type inT
that extendsU
withV
.UnionKeyOf<T>
obtains the union of the key types of each union component. This is different from applyingkeyof
to a union, which returns an intersection of the key types of each union component. Whilekeyof
returns keys that exist in every union component,UnionKeyOf
returns keys that existing in at least one (but not necessarily all) union component.OmitStrict<T, K>
drops keysK
from typeT
, just like nativeOmit
, except that here the keys are constrained to the union of keys ofT
(seeUnionKeyOf
).FitsSub<Sub, Super>
givesSub
type back, but will require that it extendsSuper
.FitsSuper<Super, Sub>
givesSuper
type back, but will require thatSub
extends from it.
Convenience Guards
isConcrete()
guards for theConcrete
typeisTruthy()
guards for the truthy type.isEnum(enumObject)
produces a function that guards for the enum provided by theenumObject
.hasKey(key)
produces a function that guards for the subset of values that are objects containing a property with the name specified inkey
.hasTag(tagOrTags, prop)
produces a function that guards for the objects containing a property named as specified inprop
, whose value is one of the tags specified. This is used to narrow discriminated unions.
Type-aware utilities
not(fn)
produces a function that returns the boolean-negated version of the original functin's result. If the given function is a guard, it produces an "anti-guard".sieve(fnMap, tagProp)
produces a function that calls a different function on the map based on the tag of input value, which is a discrimated union.asType<T>(value)
is an identity function that castsvalue
into a broader type specified inT
. This is the same as if we were to savevalue
into a temporaryconst
of typeT
, and is the opposite of doingvalue as T
, wherevalue
could be cast into a narrower type without assertion (as
is therefore unsafe).fitsType<T>(value)
is an identity function that checks thatvalue
is a subtype ofT
without casting the type ofvalue
itself intoT
.readonly(value)
is an identity function that
Test utilities
expectType(value)
orexpectType<T>()
can be used to perform type testing, using the quasi-human language and one of the four assertion symbols:ExactType
,SubType
,SuperType
andUnrelated
. Examples:
// Use types
expectType<number>().assert<number>().toBe(ExactType);
expectType<number>().assert<2>().toBe(SubType);
expectType<number>().assertBase<2>().toBe(SuperType);
expectType<number>().assert<boolean>().toBe(Unrelated);
// Or use values
expectType(10).assert(10).toBe(ExactType);
expectType(10)
.assert(2 as const)
.toBe(SubType);
expectType(10)
.assertBase(2 as const)
.toBe(SuperType);
expectType(10).assert(false).toBe(Unrelated);
// Or mix them up
expectType<number>().assert(10).toBe(ExactType);
expectType(10).assert<2>().toBe(SubType);
expectType<number>()
.assertBase(2 as const)
.toBe(SuperType);
expectType(10).assert<boolean>().toBe(Unrelated);