@soul-codes-dev/typetools
v0.3.1
Published
TypeTools are a collection of TypeScript type, type expressions and functions that aid writing idiomatic JavaScript in TypeScript a little easier.
Downloads
118
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 the.resolve()
type of the return value of a functionT
.
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);