type-plus
More than 200 type utilities for TypeScript.
Table of Contents
- Assertion Function
- assertType
- Type Guard
- Type Utilities
- Type Checking
- any
- Array
- bigint
- boolean
- function
- never
- null
- number
- numeric
- object
- Promise
- string
- symbol
- tuple
- undefined
- unknown
- void
- Constant Types
- JSON Support
- Type manipulation
- Type Predicates
- Logical
- Math
- Utility Functions
- Nominal Types
- Functional Types
- Attribution
- Useful Tips
- Similar projects
- Contribute
- Wallaby.js
Installation
npm install type-plus
yarn add type-plus
pnpm add type-plus
Assertion Function
Assertion Functions are special functions that asserts certain conditions of your program.
It is introduced in TypeScript 3.7.
They throw an error if the condition is not met, and return nothing otherwise.
These assertion functions are typically used in runtime,
so that that type of the value can be narrowed down.
assertType
assertType
provides a generic assertion function,
as well as many assertion functions for builtin types.
assertType<T>(subject)
💀 deprecated. Use assertType.as()
instead.
assertType<T>(subject, validator)
:
🚦assertion
: assert the subject
is type T
with the specified validator
.
If subject
fails the assertion,
a standard TypeError
will be thrown and provide better error info.
For example:
const s: unknown = 1
assertType<boolean>(s, s => typeof s === 'boolean')
The message beautification is provided by tersify
.
assertType.isUndefined()
🚦assertion
: assert the subject
is undefined
.
assertType.noUndefined()
🚦assertion
: assert the subject
is not undefined
.
assertType.isNull()
🚦assertion
: assert the subject
is null
.
assertType.noNull()
🚦assertion
: assert the subject
is not null
.
assertType.isNumber()
🚦assertion
: assert the subject
is number
.
assertType.noNumber()
🚦assertion
: assert the subject
is not number
.
assertType.isBoolean()
🚦assertion
: assert the subject
is boolean
.
assertType.noBoolean()
🚦assertion
: assert the subject
is not boolean
.
assertType.isTrue()
🚦assertion
: assert the subject
is true
.
assertType.noTrue()
🚦assertion
: assert the subject
is not true
.
assertType.isFalse()
🚦assertion
: assert the subject
is false
.
assertType.noFalse()
🚦assertion
: assert the subject
is not false
.
assertType.isString()
🚦assertion
: assert the subject
is string
.
assertType.noString()
🚦assertion
: assert the subject
is not string
.
assertType.isFunction()
🚦assertion
: assert the subject
is function
.
assertType.noFunction()
🚦assertion
: assert the subject
is not function
.
assertType.isError()
🚦assertion
: assert the subject
is an Error
.
assertType.noError()
🚦assertion
: assert the subject
is not an Error
.
assertType.isConstructor()
💀 deprecated. It does not work in all cases.
It passes for function that can be called with new
.
If the subject is an arrow function, it can still return true after compilation.
assertType.isNever()
🚦assertion
: assert the subject
is never
.
assertType.custom()
🚦assertion
: creates a custom assertion function.
Using it to create a custom assertion function that provides better error messages.
The message beautification is provided by tersify
.
assertType.as<T>()
🚦assertion
: assert the subject
as T
without validator.
This works similar to manual assertion ;(subject as T)
Type Guard
User-defined type guard functions is a function which its return type is specified as x is T
.
isType()
🛡️ guard
: a generic type guard function
isType.t()
💀 deprecated
: use testType.true()
instead.
isType.f()
💀 deprecated
: use testType.false()
instead.
isType.never()
💀 deprecated
: use testType.never()
instead.
isType.equal()
💀 deprecated
: use testType.equal()
instead.
Type Utilities
Equal<A, B, Then = true, Else = false>
💀 deprecated. use IsEqual
instead. This will be converted to a ↪️ parse
.
IsEqual<A, B, Then = true, Else = false>
⭕ predicate
: if A
and B
are the same.
NotEqual<A, B, Then = true, Else = false>
💀 deprecated. use IsNotEqual
instead. This will be converted to a ↪️ parse
.
IsNotEqual<A, B, Then = true, Else = false>
:
⭕ predicate
: check if A
and B
are not the same.
Extendable<A, B, Then = A, Else = never>
↪️ parse
: check if A
extends B
.
IsExtend<A, B, Then = true, Else = false>
⭕ predicate
: check if A
extends B
.
NotExtendable<A, B, Then = A, Else = never>
:
↪️ parse
: check if A
not extends B
.
IsNotExtend<A, B, Then = true, Else = false>
:
⭕ predicate
: check if A
not extends B
.
IsAssign<A, B, Then = true, Else = false>
💀 deprecated
: use CanAssign
instead.
CanAssign<A, B, Then = true, Else = false>
:
⭕ predicate
: check can A
assign to B
.
A typical usage is using it with assertType
:
assertType.isFalse(false as CanAssign<boolean, { a: string }>)
assertType.isTrue(true as CanAssign<{ a:string, b:number }, { a: string }>)
StrictCanAssign<A, B, Then = true, Else = false>
⭕ predicate
: can A
strictly assign to B
When A
is a union, all branches must be assignable to B
.
StrictCanAssign<number | string, number>
StrictCanAssign<number | string, number | string>
canAssign<T>(): (subject) => true
⭕💻 predicate
, compile-time
Returns a compile-time validating function to ensure subject
is assignable to T
.
const isConfig = canAssign<{ a: string }>()
assertType.isTrue(isConfig({ a: 'a' }))
canAssign<T>(false): (subject) => false
:
⭕💻 predicate
, compile-time
Returns a compile-time validating function to ensure subject
is not assignable to T
.
const notA = canAssign<{ a: string }>(false)
assertType.isTrue(notA({ a: 1 }))
notA({ a: '' })
Type Checking
type-plus privides type checking utilities for every type.
Each type has at least 4 type checks.
Using string
as an example, there are StringType<T>
, IsString<T>
, NotStringType<T>
, and IsNotString<T>
.
Some types will have more checks, such as boolean
has StrictBooleanType<T>
, TrueType<T>
, FalseType<T>
.
You can learn more in their respective sections:
any
AnyType<T>
↪️ parse
: T === any
.
IsAny<T>
⭕ predicate
: T === any
.
NotAnyType<T>
↪️ parse
: T !== any
.
IsNotAny<T>
⭕ predicate
: T !== any
.
IsAnyOrNever<T>
⭕ predicate
: T === any || T === never
.
Array
ArrayType<T>
↪️ parse
: is the type T
exactly an array and not a tuple.
IsArray<T>
⭕ predicate
: is the type T
exactly an array and not a tuple.
NotArrayType<T>
↪️ parse
: is the type T
not an array or tuple.
IsNotArray<T>
⭕ predicate
: is the type T
exactly an array and not a tuple.
At<A, N, Fail = never>
🔨 utilities
: gets the element type at index N
in the array A
.
CommonPropKeys<A>
🔨 utilities
: gets common keys inside the records in the array A
(deprecate CommonKeys
).
Concat<A, B>
🔨 utilities
: [...A, ...B]
.
CreateTuple<L, T>
🔨 utilities
: creates tuple<T>
with L
number of elements.
drop(array, value)
🔨 utilities
: drop a particular value from an array.
DropFirst<A>
🔨 utilities
: drops the first value type of A
.
DropLast<A>
🔨 utilities
: drops the last value type of A
.
DropMatch<A, Criteria>
🔨 utilities
: drops entries matching Criteria
in array or tuple A
.
DropUndefined<A>
🔨 utilities
: drop undefined entries from array of tuple A
.
Filter<A, Criteria>
💀🔨 deprecated
,utilities
: filter the array or tuple A
, keeping entries satisfying Criteria
. Deprecated. Renaming to KeepMatch
FindFirst<A, Criteria>
🔨 utilities
: gets the first type satisfying Criteria
.
FindLast<A, Criteria>
🔨 utilities
: gets the last type satisfying Criteria
.
Head<A>
🔨 utilities
: gets the first entry in the array.
IntersectOfProps<A, K>
🔨 utilities
: gets the intersect of A[K]
types (deprecate MapToProp
)
KeepMatch<A, Criteria>
🔨 utilities
: keeps entries satisfying Criteria
in array or tuple A
.
Last<A>
🔨 utilities
: gets the last type of array or tuple.
literalArray(...entries)
🔨 utilities
: return an array whose items are restricted to the provided literals.
PadLeft<A, Total, PadWith>
🔨 utilities
: pads A
with PadWith
if the length of A
is less than L
.
reduceWhile()
🔨 utilities
: reduce()
with predicate for early termination.
A simple version of the same function in the ramda
package.
Reverse<A>
🔨 utilities
: reverses the order of A
.
Some<A, Criteria>
🔨 utilities
: true if some elements in A
matches Criteria
.
Tail<A>
🔨 utilities
: Gets the types of a tuple except the first entry.
UnionOfProps<A, K>
🔨 utilities
: gets the union of A[K]
types (deprecate PropUnion
).
UnionOfValues<A>
🔨 utilities
: gets the union of value types in A
(deprecate ArrayValue
).
ArrayPlus.IndexAt<A, N, Fail = never>
🔨 utilities
: gets the normalized index for A
.
ArrayPlus.IsIndexOutOfBound<A, N, Then = true, Else = false>
⭕ predicate
: is N
an out of bound index of A
. Supports negative numbers.
bigint
BigintType<T, Then = T, Else = never>
↪️ parse
: if T
is bigint
or bigint literal.
IsBigint<T, Then = true, Else = false>
⭕ predicate
: if T
is bigint
or bigint literal.
NotBigintType<T, Then = T, Else = never>
↪️ parse
: if T
is not bigint
or bigint literal.
IsNotBigInt<T, Then = true, Else = false>
⭕ predicate
: if T
is not bigint
or bigint literal.
StrictBigintType<T, Then = T, Else = never>
↪️ parse
: if T
is exactly bigint
.
IsStrictBigint<T, Then = true, Else = false>
⭕ predicate
: if T
is exactly bigint
.
NotStrictBigintType<T, Then = T, Else = never>
↪️ parse
: if T
is not exactly bigint
.
IsNotStrictBigint<T, Then = true, Else = false>
⭕ predicate
: if T
is not exactly bigint
.
boolean
BooleanType<T>
↪️ parse
: T === boolean
.
IsBoolean<T>
⭕ predicate
: T === boolean
NotBooleanType<T>
↪️ parse
: T !== boolean
.
IsNotBoolean<T>
⭕ predicate
: T !== boolean
function
FunctionType<T>
↪️ parse
: T === function
.
IsFunction<T>
⭕ predicate
: T === function
NotFunctionType<T>
↪️ parse
: T !== function
.
IsNotFunction<T>
⭕ predicate
: T !== function
AnyFunction<P, R>
🔨 utilities
: a generic type for any function
ExtractFunction<F>
🔨 utilities
: extract the function signature from a type F
.
extractFunction(fn: F)
🔨 utilities
: adjust type of fn
to its function signature only.
inspect<T>(value: T, inspector?: (v: T) => void)
🔨 utilities
: inspect a value and return it. Inspector defaults to console.dir()
never
NeverType<T>
↪️ parse
: T === never
.
IsNever<T>
⭕ predicate
: T === never
NotNeverType<T>
↪️ parse
: T !== never
.
IsNotNever<T>
⭕ predicate
: T !== never
null
NullType<T>
↪️ parse
: T === null
.
IsNull<T>
⭕ predicate
: T === null
NotNullType<T>
↪️ parse
: T !== null
.
IsNotNull<T>
⭕ predicate
: T !== null
number
NumberType<T, Then = N, Else = never>
↪️ parse
: is the type T
number
.
IsNumber<T, Then = true, Else = false>
⭕ predicate
: is the type T
number
.
NotNumberType<T, Then = T, Else = never>
↪️ parse
: is the type T
not number
.
IsNotNumber<T, Then = true, Else = false>
⭕ predicate
: is the type T
not number
.
StrictNumberType<T, Then = N, Else = never>
↪️ parse
: is the type T
exactly number
.
IsStrictNumber<T, Then = true, Else = false>
⭕ predicate
: is the type T
exactly number
.
NotStrictNumberType<T, Then = T, Else = never>
↪️ parse
: is the type T
not exactly number
.
IsNotStrictNumber<T, Then = true, Else = false>
⭕ predicate
: is the type T
not exactly number
.
numeric
Numeric
📘 definition
: number | bigint
.
Zero
📘 definition
: 0 | 0n
Integer<N, Then = N, Else = never>
↪️ parse
: is integer.
IsInteger<N, Then = true, Else = false>
⭕ predicate
: is integer.
NotInteger<N, Then = N, Else = never>
↪️ parse
: is not integer.
IsNotInteger<N, Then = true, Else = false>
⭕ predicate
: is not integer.
IsWhole<N, Then = true, Else = false>
💀⭕ deprecated
, predicate
: is integer. Use IsInteger
instead.
Negative<N, Then = N, Else = never>
↪️ parse
: is negative.
IsNegative<N, Then = true, Else = false>
⭕ predicate
: is negative.
NonNegative<N, Then = N, Else = never>
↪️ parse
: is not negative.
IsNonNegative<N, Then = N, Else = never>
⭕ predicate
: is not negative.
Positive<N, Then = N, Else = never>
↪️ parse
: is positive.
IsPositive<N, Then = true, Else = false>
⭕ predicate
: is positive.
NotPositive<N, Then = N, Else = never>
↪️ parse
: is not positive.
IsNotPositive<N, Then = true, Else = false>
⭕ predicate
: is not positive.
object
filterKey()
🔨 utilities
: type adjusted filter by key.
findKey()
🔨 utilities
: type adjusted find by key.
forEachKey()
🔨 utilities
: type adjusted for each by key.
HasKey<T, K>
🔨 utilities
: predicate type checking T
has key K
.
hasKey()
🔨 utilities
: function of HasKey
.
IsRecord<T>
🔨 utilities
: logical
predicate for Record
.
KeysWithDiffTypes<A, B>
🔨 utilities
: gets the keys common in A
and B
but with different value type.
mapKey()
🔨 utilities
: type adjusted map by key.
RecordValue<R>
🔨 utilities
: gets the value type T
from Record<any, T>
video.
reduceByKey()
🔨 utilities
: type adjusted reduce by key.
someKey()
🔨 utilities
: type adjusted some by key.
SpreadRecord<A, B>
🔨 utilities
: type for {...a, ...b}
when both a
and b
are Record
for array, just do [...A, ...B]
.
Promise
AwaitedProp<T, V>
🔨 utilities
: Awaited
on specified props P
in T
.
isPromise<R>(subject: any)
🔨 utilities
: isPromise()
type guard.
MaybePromise<T>
🔨 utilities
: Alias of T | Promise<T>
.
PromiseValue<P>
🔨 utilities
: Gets the type within the Promise.
PromiseValueMerge<P1, P2, ...P9>
🔨 utilities
: Merge the values of multiple promises.
mapSeries()
🔨 utilities
: Similar to bluebird.mapSeries()
but works with async
/await
.
transformMaybePromise(value, transformer)
🔨 utilities
: Apply the transformer
to the value
.
It is also exported under MaybePromise.transform()
.
string
StringType<T>
↪️ parse
: is string
.
IsString<T>
⭕ predicate
: is string
.
NotStringType<T>
↪️ parse
: is not string
.
IsNotString<T>
⭕ predicate
: is not string
.
symbol
SymbolType<T>
↪️ parse
: is symbol
.
IsSymbol<T>
⭕ predicate
: is symbol
.
NotSymbolType<T>
↪️ parse
: is not symbol
.
IsNotSymbol<T>
⭕ predicate
: is not symbol
.
tuple
TupleType<T>
↪️ parse
: is a tuple.
IsTuple<T>
⭕ predicate
: is a tuple.
NotTupleType<T>
↪️ parse
: is not a tuple.
IsNotTuple<T>
⭕ predicate
: is not a tuple.
undefined
UndefinedType<T>
↪️ parse
: T === undefined
.
IsUndefined<T>
⭕ predicate
: T === undefined
NotUndefinedType<T>
↪️ parse
: T !== undefined
.
IsNotUndefined<T>
⭕ predicate
: T !== undefined
unknown
UnknownType<T>
↪️ parse
: T === unknown
.
IsUnknown<T>
⭕ predicate
: T === unknown
NotUnknownType<T>
↪️ parse
: T !== unknown
.
IsNotUnknown<T>
⭕ predicate
: T !== unknown
void
VoidType<T>
↪️ parse
: T === void
.
IsVoid<T>
⭕ predicate
: T === void
NotVoidType<T>
↪️ parse
: T !== void
.
IsNotVoid<T>
⭕ predicate
: T !== void
Constant Types
KeyTypes
📘 definition
: type of all keys.
PrimitiveTypes
📘 definition
: all primitive types, including Function
, symbol
, and bigint
.
ComposableTypes
📘 definition
: Types that can contain custom properties. i.e. object
, array
, function
.
NonComposableTypes
📘 definition
: Types that cannot contain custom properties. i.e. not composable.
JSON Support
JSONPrimitive
📘 definition
: primitive types valid in JSON
JSONObject
📘 definition
: JSON object
JSONArray
📘 definition
: JSON array
JSONTypes
📘 definition
: all JSON compatible types.
JSONTypes.get<T>(obj, ...props)
🔨 utilities
: get a cast value in JSON
import { JSONTypes } from 'type-plus'
const someJson: JSONTypes = { a: { b: ['z', { c: 'miku' }]}}
JSONTypes.get<string>(someJson, 'a', 'b', 1, 'c')
Type manipulation
ANotB<A, B>
🔨 utilities
: get object with properties in A
and not in B
, including properties with a different value type.
BNotA<A, B>
🔨 utilities
: flip of ANotB
as<T>(subject)
🔨 utilities
: assert subject
as T
. Avoid ASI issues such as ;(x as T).abc
asAny(subject)
🔨 utilities
: assert subject
as any
. Avoid ASI issue such as ;(x as any).abc
EitherAnd<A, B, [C, D]>
💀🔨 deprecated
,utilities
: Renamed to EitherOrBoth
. combines 2 to 4 types as A | B | (A & B)
.
This is useful for combining options.
EitherOrBoth<A, B, [C, D]>
🔨 utilities
: combines 2 to 4 types as A | B | (A & B)
.
This is useful for combining options video.
Except<T, K>
💀🔨 deprecated
,utilities
: same as Omit<T, K>
.
ExcludePropType<T, U>
🔨 utilities
: excludes type U
from properties in T
.
KeyofOptional<T>
🔨 utilities
: keyof
that works with Record<any, any> | undefined
.
KnownKeys<T>
🔨 utilities
: extract known (defined) keys from type T
.
LeftJoin<A, B>
🔨 utilities
: left join A
with B
NonNull<T>
🔨 utilities
: remove null
NonNullable<T>
(built-in)
🔨 utilities
: adjust the type not to nullable
NonUndefined<T>
🔨 utilities
: remove undefined
Omit<T, K>
🔨 utilities
: From T
, pick a set of properties whose keys are not in the union K
. This is the opposite of Pick<T, K>
.
OptionalKeys<T>
🔨 utilities
: gets keys of optional properties in T
.
PartialExcept<T, U>
💀🔨 deprecated
,utilities
: same as PartialOmit<T, U>
.
PartialOmit<T, U>
🔨 utilities
: makes the properties not specified in U
becomes optional.
PartialPick<T, U>
🔨 utilities
: makes the properties specified in U
becomes optional.
Pick<T, K>
🔨 utilities
: pick properties K
from T
. Works with unions.
RecursivePartial<T>
🔨 utilities
: make type T
optional recursively.
RecursiveRequired<T>
🔨 utilities
: make type T
required recursively.
ReplaceProperty<T, K, V>
🔨 utilities
: replace property K
in T
with V
.
RequiredKeys<T>
🔨 utilities
: gets keys of required properties in T
.
RequiredPick<T, U>
🔨 utilities
: makes the properties specified in U
become required.
RequiredExcept<T, U>
🔨 utilities
: makes the properties not specified in U
become required.
RecursiveIntersect<T, U>
🔨 utilities
: intersect type U
onto T
recursively.
ValueOf<T>
🔨 utilities
: type of the value of the properties of T
.
Widen<T>
🔨 utilities
: widen literal types.
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>
🔨 utilities
: predicate type checking T
has key K
.
IsAny<T>
🔨 utilities
: T === any
(updated to impl: expect-type).
IsBoolean<T>
🔨 utilities
: check for boolean
, but not for true
nor false
.
IsDisjoint<A, B>
🔨 utilities
: is A
and B
is a disjoint set.
IsEmptyObject<T>
🔨 utilities
: is T === {}
.
IsLiteral<T>
🔨 utilities
: is T
a literal type (literal string or number).
Logical
If<Condition, Then = true, Else = false>
🔨 utilities
: if statement
And<A, B, Then = true, Else = false>
🔨 utilities
: logical AND
Or<A, B, Then = true, Else = false>
🔨 utilities
: logical OR
Xor<A, B, Then = true, Else = false>
🔨 utilities
: logical XOR
Not<X, Then = true, Else = false>
🔨 utilities
: logical NOT
Note that these types work correctly with the boolean
type.
e.g.:
type R = And<boolean, true>
type R = Not<boolean>
There is a problem with generic distribution: https://github.com/microsoft/TypeScript/issues/41053
So you may encounter some weird behavior if your logic is complex.
Math
The math types in type-plus
works with most numeric types.
It works with number
and bigint
, positive and negative number, including floating point numbers.
It will cast the type between number
and bigint
if needed.
Abs<N, Fail = never>
🔨 utilities
: Abs(N)
.
Max<A, B, Fail = never>
🔨 utilities
: max(A, B)
GreaterThan<A, B>
🔨 utilities
: A > B
.
Add<A, B>
🔨 utilities
: A + B
.
Subtract<A, B>
🔨 utilities
: A > B
.
Increment<A>
🔨 utilities
: alias of Add<A, 1>
.
Decrement<A>
🔨 utilities
: alias of Subtract<A, 1>
.
Multiply<A, B
🔨 utilities
: A * B
.
Utility Functions
amend(subject)...
🔨 utilities
: amend subject as union or intersect of T
.
facade(subject, ...props)
🔨 utilities
: create a facade of subject
.
getField(subject, key, defaultValue)
🔨 utilities
: get a field from a subject. Works against nullable and optional subject.
hasKey()
🔨 utilities
: function of HasKey
.
hasProperty(value, prop)
🔨 utilities
: assert value
has property prop
. This will pick the correct union type.
isConstructor(subject)
🔨 utilities
: type guard subject
is a constructor.
isSystemError(code, err)
🔨 utilities
: type guard err
with NodeJS error code.
omit(obj, ...props)
🔨 utilities
: omit properties from obj
.
pick(obj, ...props)
🔨 utilities
: pick properties from obj
.
record<K, V>(value?)
🔨 utilities
: create a Record<K, V>
without extra object prototype.
record<R>(value?)
🔨 utilities
: create a record R
(e.g. { a: number }
) without extra object prototype.
required(...)
🔨 utilities
: merge options and remove Partial<T>
. From unpartial
requiredDeep(...)
🔨 utilities
: merge options deeply and remove Partial<T>
. From unpartial
split(target, ...splitters)
🔨 utilities
: split one object into multiple objects.
stub<T>(value)
🔨 utilities
: stub a particular type T
.
stub.build<T>(init?)
🔨 utilities
: build a stub for particular type T
.
typeOverrideIncompatible<T>()
🔨 utilities
: override only the incompatible portion between two types.
type A = {
foo: boolean,
bar: string,
baz: string
}
const overrider = typeOverrideIncompatible<A>()
const source = {
foo: 1,
bar: 'bar',
baz: 'baz'
}
overrider(source, { foo: !!source.foo })
unpartial()
🔨 utilities
: merge options and remove Partial<T>
values. From unpartial
context()
🔨 utilities
: a context builder.
This is useful to build context for functional programming.
It is a sync version of the AsyncContext
from async-fp.
import { context } from 'type-plus'
const ctx = context({ a: 1 })
.extend(c => ({ b: c.a + 1 }))
.build()
Nominal Types
The TypeScript type system is structural.
In some cases, we want to express a type with nominal behavior.
type-plus
provides two kinds of nominal types: Brand
and Flavor
.
Brand<B, T>
:
brand(type, subject?)
:
Branded nominal type is the stronger nominal type of the two.
It disallows unbranded type assigned to it:
const a = brand('a', { a: 1 })
const b = { a: 1 }
a = b
subject
can be any type, from primitive to strings to objects.
brand(type)
:
If you do not provide subject
, brand(type)
will return a brand creator,
so that you can use it to create multiple branded values:
const nike = brand('nike')
const shirt = nike('shirt')
const socks = nike('socks')
Flavor<F, T>
:
flavor(type, subject?)
:
The key difference between Flavor
and Brand
is that
unflavored type can be assigned to Flavor
:
let f = flavor('orange', 'soda')
f = 'mist'
Also, Brand
of the same name can be assigned to Flavor
,
but Flavor
of the same name cannot be assigned to Brand
.
nominalMatch(a, b)
:
🔨 utilities
: compare if the two values are nominally equal.
Works with both Brand
and Flavor
.
const b1 = brand('x', 1)
const b2 = brand('y', 1)
nominalMatch(b1, b2)
Functional Types
ChainFn<T>: T
🔨 utilities
: chain function that returns the input type.
compose(...fns): F
🔨 utilities
: compose functions
Attribution
Some code in this library is created by other people in the TypeScript community.
I'm merely adding them in and maybe making some adjustments.
Whenever possible, I add attribution to the person who created those codes in the file.
Useful Tips
https://github.com/microsoft/TypeScript/wiki/Performance
Similar projects
- expect-type: Compile-time tests for types
- hotscript: Higher-order TypeScript
- spec.ts: write tests for your types!
- ts-essentials: all essential TypeScript types in one place.
- ts-expect: Checks values in TypeScript match expectations.
- ts-toolbelt: TypeScript's largest utility library.
- type-fest: a collection of essential TypeScript types.
- type-zoo: a modest type lib usable today.
- typepark: a new type collection offering tuple manipulation and
Pipe
. - typelevel-ts: a type lib by @gcanti, author of several FP libraries in TS.
- typical: a playground of type-level operations for TypeScript.
- utility-types: collection of utility types, complementing TypeScript build-in mapped types ans aliases.
Contribute
npm install
git checkout -b <branch>
npm run watch
git commit -m "<commit message>"
git push
Wallaby.js
This repository contributors are welcome to use
Wallaby.js OSS License to get
test results immediately as you type, and see the results in
your editor right next to your code.