1.1.0 • Published 3 years ago
ts-typetools v1.1.0
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 theresolve()type of the return value of a functionT, otherwiseTitself.MaybeArray<T>is a shortcut forT | Array<T>.ArrayItemType<T>obtains the element type of an arrayT, otherwiseTitself.ExtractRefine<T, U, V>refines part of typeTthat extendsU(like the built-inExtracttype) by interesecting that extraction with theVtype.ExtractOmit<T, U, K>selectively omits propertiesKfrom a part of typeTthat extendsU.ExtractReplace<T, U, V>selectively replaces a type inTthat extendsUwithV.UnionKeyOf<T>obtains the union of the key types of each union component. This is different from applyingkeyofto a union, which returns an intersection of the key types of each union component. Whilekeyofreturns keys that exist in every union component,UnionKeyOfreturns keys that existing in at least one (but not necessarily all) union component.OmitStrict<T, K>drops keysKfrom typeT, just like nativeOmit, except that here the keys are constrained to the union of keys ofT(seeUnionKeyOf).FitsSub<Sub, Super>givesSubtype back, but will require that it extendsSuper.FitsSuper<Super, Sub>givesSupertype back, but will require thatSubextends from it.
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);