type-plus
Provides additional types and type adjusted utilities for typescript
API
Identity type
Id<T>: generic Id typecreateId<T>(type: T, value: string): Id<T>: create idcreateIdCreator<T>(type: T): (value: string) => Id<T>: create an id creator
Constant Types
JSONTypes: all JSON compatible types.KeyTypes: type of all keys.PrimitiveTypes: all primitive types, includingFunction,symbol, andbigint
Object Key functions
filterKey(): type adjusted filter by key.findKey(): type adjusted find by key.forEachKey(): type adjusted for each by key.HasKey<T, K>: predicate type checkingThas keyK.hasKey(): function ofHasKey.KeysWithDiffTypes<A, B>: gets the keys common inAandBbut with differnt value type.mapKey(): type adjusted map by key.reduceKey(): type adjusted reduce by key.someKey(): type adjusted some by key.
Array function
literalArray(...entries): return an array those items are restricted to the provided literals.
Promise function
isPromise<R>(subject: any):isPromise()type guard.PromiseValue<P>: Gets the type within the Promise.PromiseValueMerge<P1, P2, ...P9>: Merge the values of multiple promises.mapSeries(): Similar tobluebird.mapSeries()but works withasync/await.
Nominal Type
Flavor<FlavorT, T>: a flavored nominal type.Brand<BrandT, T>: a branded nominal type.createBrandCreator<BrandT, T>(): creates a brand creator to create branded nominal type.
Type manipulation
ANotB<A, B>: get object with properties inAand not inB, including properties with differnt value type.BNotA<A, B>: flip ofANotBExcept<T, K>: Deprecated. Same asOmit<T, K>.ExcludePropType<T, U>: excludes typeUfrom properties inT.KeyofOptional<T>:keyofthat works withRecord<any, any> | undefined.KnownKeys<T>: extract known (defined) keys from typeT.LeftJoin<A, B>: left joinAwithBOmit<T, K>: FromT, pick a set of properties whose keys are not in the unionK. This is the opposite ofPick<T, K>.PartialExcept<T, U>: Deprecated. Same asPartialOmit<T, U>.PartialOmit<T, U>: makes the properties not specified inUbecomes optional.PartialPick<T, U>: makes the properties specified inUbecomes optional.RecursivePartial<T>: make typeToptional recursively.RecursiveRequired<T>: make typeTrequired recursively.ReplaceProperty<T, K, V>: replace propertyKinTwithV.RequiredPick<T, U>: makes the properties specified inUbecomes required.RequiredExcept<T, U>: makes the properties not specified inUbecomes required.RecursiveIntersect<T, U>: intersect typeUontoTrecursively.ValueOf<T>: type of the value of the properties ofT.- PropType: ...no helper type for this. Just do
YourType['propName'].
Type Predicates
Type predicates are type alias that returns true or false.
They can be used to compose complex types.
HasKey<T, K>: predicate type checkingThas keyK.IsDisjoint<A, B>: isAandBis a disjoint set.IsSame<A, B>: isAandBhas the same properties and types.Not<T>: not operator for type.
Type assertion
assignability<T>(handler?: (s: any) => boolean): creates a assignability function. To use it, do the following:
assertType.isTrue(assignability<YourType>()(subject)) // or
assertType.isFalse(assignability<YourType>()(subject))assertType<T>(subject: T): assertsubjectsatisfies typeT.assertType.isXXX(value): ensure typeofvalueisXXXassertType.noXXX(value): ensure typeofvaluedoes not containXXX. i.e. cannot assignXXXtovalue.assertUnknown<T>(subject: unknown, handler?: (s: T) => boolean): assertunknownsubjectsatisfies typeT.checkUnknown<T>(subject: unknown, handler: overloads): type guardunknownsubjectis a specific typeTtypeAssert.*(deprecated) replaced byassertType.typeAssertion<T>(): (deprecated) useassertType()instead.
Utility Functions
facade(subject, ...props): create a facade ofsubject.getField(subject, key, defaultValue): get a field from a subject. Works against nullable and optional subject.hasKey(): function ofHasKey.hasProperty(value, prop): assertvaluehas propertyprop. This will pick the correct union type.isConstructor(subject): type guardsubjectis a constructor.pick(obj, ...props): pick properties fromobj.omit(obj, ...props): omit properties fromobj.required(...): merge options and removingPartial<T>. FromunpartialrequiredDeep(...): merge options deeply and removingPartial<T>. FromunpartialtryAssign<S, T>(from: S, to: T): try assignfromtoto. Return typeneverif not possible.typeOverrideIncompatible<T>(): override only the incompatiable portion between two types.
type A = {
foo: boolean,
bar: string,
baz: string
}
const overrider = typeOverrideIncompatible<A>()
const source = {
foo: 1,
bar: 'bar',
baz: 'baz'
}
// only the `foo` property is available to override.
overrider(source, { foo: !!source.foo })Attribution
Some of the code in this library are created by other people in the TypeScript community. I merely adding them in and may be making some adjustments. When ever possible, I add attribution to the person who created those code in the file.
Contribute
# after fork and clone
npm install
# begin making changes
git checkout -b <branch>
npm run watch
# after making change(s)
git commit -m "<commit message>"
git push
# create PR