Provides additional types and type adjusted utilities for typescript
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
JSONTypes
: all JSON compatible types.KeyTypes
: type of all keys.PrimitiveTypes
: all primitive types, includingFunction
,symbol
, andbigint
filterKey()
: type adjusted filter by key.findKey()
: type adjusted find by key.forEachKey()
: type adjusted for each by key.HasKey<T, K>
: predicate type checkingT
has keyK
.hasKey()
: function ofHasKey
.KeysWithDiffTypes<A, B>
: gets the keys common inA
andB
but with differnt value type.mapKey()
: type adjusted map by key.reduceKey()
: type adjusted reduce by key.someKey()
: type adjusted some by key.
literalArray(...entries)
: return an array those items are restricted to the provided literals.
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
.
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.
ANotB<A, B>
: get object with properties inA
and not inB
, including properties with differnt value type.BNotA<A, B>
: flip ofANotB
Except<T, K>
: Deprecated. Same asOmit<T, K>
.ExcludePropType<T, U>
: excludes typeU
from properties inT
.KeyofOptional<T>
:keyof
that works withRecord<any, any> | undefined
.KnownKeys<T>
: extract known (defined) keys from typeT
.LeftJoin<A, B>
: left joinA
withB
Omit<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 inU
becomes optional.PartialPick<T, U>
: makes the properties specified inU
becomes optional.RecursivePartial<T>
: make typeT
optional recursively.RecursiveRequired<T>
: make typeT
required recursively.ReplaceProperty<T, K, V>
: replace propertyK
inT
withV
.RequiredPick<T, U>
: makes the properties specified inU
becomes required.RequiredExcept<T, U>
: makes the properties not specified inU
becomes required.RecursiveIntersect<T, U>
: intersect typeU
ontoT
recursively.ValueOf<T>
: type of the value of the properties ofT
.- PropType: ...no helper type for this. Just do
YourType['propName']
.
Type predicates are type alias that returns true
or false
.
They can be used to compose complex types.
HasKey<T, K>
: predicate type checkingT
has keyK
.IsDisjoint<A, B>
: isA
andB
is a disjoint set.IsSame<A, B>
: isA
andB
has the same properties and types.Not<T>
: not operator for type.
assignability<T>()
: creates a assignability function. To use it, do the following:
assertType.isTrue(assignability<YourType>()(subject)) // or
assertType.isFalse(assignability<YourType>()(subject))
assertType<T>(subject)
: assertsubject
satisfies typeT
.assertType.isXXX(value)
: ensure typeofvalue
isXXX
assertType.noXXX(value)
: ensure typeofvalue
does not containXXX
. i.e. cannot assignXXX
tovalue
.typeAssert.*
(deprecated) replaced byassertType
.typeAssertion<T>()
: creates a type assertion function of typeT
type Foo = { a: number }
const assertIsFoo = typeAssertion<Foo>()
function createFoo() {
return assertIsFoo({ a: 1, b: 2 })
}
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)
: assertvalue
has propertyprop
. This will pick the correct union type.pick(obj, ...props)
: pick properties fromobj
.omit(obj, ...props)
: omit properties fromobj
.required(...)
: merge options and removingPartial<T>
. Fromunpartial
requiredDeep(...)
: merge options deeply and removingPartial<T>
. Fromunpartial
tryAssign<S, T>(from: S, to: T)
: try assignfrom
toto
. Return typenever
if 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 })
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.
# 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