@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.
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
Falsyrepresents the type of all falsy values.NullLikerepresents thenullandundefinedtypes.Keyrepresents the type of all valid keys, namelynumber,stringandsymbol.Primitiverepresents the type of all primitive (not object) values, namelystring,number,boolean,symbol,null,undefined.Concreterepresents the type of everything that is notnullorundefined, (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 theConcretetypeisTruthy()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 castsvalueinto a broader type specified inT. This is the same as if we were to savevalueinto a temporaryconstof typeT, and is the opposite of doingvalue as T, wherevaluecould be cast into a narrower type without assertion (asis therefore unsafe).fitsType<T>(value)is an identity function that checks thatvalueis a subtype ofTwithout casting the type ofvalueitself 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,SuperTypeandUnrelated. 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);