type-ts
Typescript Type-level playground
This project is a playground of type-level operations for TypeScript.
Installation
npm i type-ts
TypeScript compatibility
The stable version is tested against TypeScript 3.1.6+.
API
Logic
Extends<A, B>
Returns true
if A
extends from B
, false
otherwise.
Extends<'foo', string> // trueExtends<string, number> // false
Equals<A, B>
Returns true
if A
and B
are equal types, false
otherwise.
Equals<string, string> // trueEquals<string, number> // false
And<A, B>
Returns true
if A
and B
are equal types to true
, false
otherwise.
And<true, true> // trueAnd<true, false> // false
Or<A, B>
Returns true
if either A
or B
are true
, false
otherwise.
Or<false, true> // trueOr<false, false> // false
Xor<A, B>
Returns true
if A
and B
differ, one is true
and the other is false
. false
otherwise.
Xor<false, true> // trueXor<true, true> // false
Not<A>
Returns true
if A
is false
. false
otherwise.
Not<false> // trueNot<true> // false
If<C, T, E>
Returns T
if C
is true
. E
otherwise.
If<true, number, string> // numberIf<false, number, string> // string
Newtype
Newtype<T, NAME>
Same representation, different type for the typesystem.
;; ;; grossusd, usd; // $ExpectType Newtype<number, "USD">// $ExpectErrorgrosseur, usd; // Type '"EUR"' is not assignable to type '"USD"'.// $ExpectErrorgross1, 12; // Type 'number' is not assignable to type '"USD"'.
Type
Omit<A, K>
Extracts a super-type of A
identified by its keys K
.
Omit<, 'a'> // { b: number }
Overwrite<A, B>
Overwrites A
with the properties in B
.
Overwrite<,> // { a: string; b: boolean }
Diff<A, K>
Make the specified properties K
partial in type A
.
Diff<, 'b'> // { a: string; b?: number }
Lacks<A, K>
Encodes the constraint that a given object A
does not contain specific keys K
declare ;// $ExpectErrorf;
Exact<A>
Encodes that A
cannot have extra propeties.
;declare ;declare ;declare ;declare ;fx; // $ExpectErrorfy; // Okfz; // $ExpectError
KeysOfType<A, B>
Picks from A
only the keys of a certain type B
.
KeysOfType<, string> // "a" | "d"
DeepReadonly<A>, DeepMutable<A>, DeepRequired<A>, DeepPartial<A>
Recursively sets modifiers readonly
and ?
.
;declare ;a.b.d.e = 1; // $ExpectError
Intersection<A, B>
Extracts from A
only the properties found on B
.
Intersection<,> // { b: number }
ValuesOf<A>
Returns the union of all value types from A
.
ValuesOf<> // number | boolean | string
SetIntersection<A, B>
Intersection of the literal union sets A
and B
.
SetIntersection<"a" | "b", "c" | "d"> // neverSetIntersection<"a" | "b", "b" | "c"> // "b"SetIntersection<"a" | "b" | "c" , "b" | "c"> // "b" | "c"
SetDifference<A, B>
Difference of the literal union sets A
and B
.
SetDifference<"a" | "b", "c"> // "a" | "b"SetDifference<"a" | "b", "b"> // "a"
SetUnion<A, B>
Union of the literal union sets A
and B
.
SetUnion<"a" | "b", "b" | "c"> // "a" | "b" | "c"
Pretty<A>
Display purpouse only. Useful when dealing with complex types and want to see something simpler in the IDE.
Pretty< & > // { a: 1, b: 2 }
NoInfer<A>
Use to prevent a usage of type T
from being inferred in other generics. See
JSONValue
Represents JSON representable types.
Awaited<A>
Unwraps nested Promise
s. Typescript does not collapse nested promises. See
; // Promise<number>; // Promise<string>; // Promise<boolean>
Number
Peano numbers implementation.
Zero
Zero
Succ<N>
Succ<Zero> // _1
Digit = _0 | _1 | _2 | _3 | _4 | _5 | _6 | _7 | _8 | _9
Predefined aliases.
Add<A, B>
Add<_4, _3> // _7Add<_5, _4> // _9
Substract<A, B>
Substract<_0, _1> // neverSubstract<_4, _3> // _1Substract<_5, _2> // _3
Multiply<A, B>
Multiply<_2, _3> // _6Multiply<_3, _3> // _9
Quotient<A, B>
Quotient<_4, _0> // neverQuotient<_3, _3> // _1Quotient<_0, _6> // _0Quotient<_4, _2> // _2Quotient<_6, _5> // _1
Quotient<A, B>
Quotient<_3, _3> // _0Quotient<_5, _3> // _2Quotient<_4, _2> // _0Quotient<_6, _5> // _1
GreaterThan<A, B>
GreaterThan<_1, _0> // trueGreaterThan<_5, _2> // trueGreaterThan<_0, _0> // falseGreaterThan<_0, _1> // false
LowerThan<A, B>
LowerThan<_1, _0> // falseLowerThan<_5, _2> // falseLowerThan<_0, _0> // falseLowerThan<_0, _1> // true
EqualsTo<A, B>
EqualsTo<_0, _1> // falseEqualsTo<_2, _2> // true
AsNat<N>
Convert a number
to a Nat
. Do not use with negative numbers.
AsNat<2> // _2 or Succ<Succ<_0>>
AsNumbe<N>
Convert a Nat
to a number
.
AsNumber<_2> // 2
List
Tuple<A>
Non empty tuple.
Nil
Empty tuple.
StaticArray<A>
Compile time arrays (aka array literals).
AnyStaticArray
Alias for StaticArray<any>
.
Head<A>
Returns the first type of the static array.
Head<> // neverHead<> // 1Head<> // number
Tail<A>
Tail of the static array.
Tail<> // neverTail<> // [2]Tail<> // [2, 3]
Last<A>
Returns the last type of the static array.
Last<> // neverLast<> // 2Last<> // 3
Cons<H, L>
Adds a type to the front of the static array.
Cons<1, > // [1]Cons<1, Cons<2, >> // [1, 2]Cons<1, > // [1, 2, 3]
Drop<A, N>
Drops N
elements from the front of the static array.
Drop<, _2> // []Drop<, _1> // [2]Drop<, _2> // [3]
Reverse<A>
Reverses a static array.
Reverse<> // []Reverse<> // [2, 1]Reverse<> // [3, 2, 1]
Take<A, N>
Return the first N
types of the static array.
Take<, _2> // []Take<, _1> // [1]Take<, _2> // [1, 2]
Concat<A, B>
Concatenates two static arrays.
Concat<, > // []Concat<, > // [1]Concat<, > // [2, 1]Concat<, > // [1, 2, 3]Concat<, > // [1, 2, 3]
Zip<A, B>
Zips two static arrays into one with length equals to the shortest length where at each position there is a tuple with the types of each one of them.
Zip<, > // []Zip<, > // []Zip<, > // [[2, 1]]Zip<, > // [[1, 3]]Zip<, > // [[1, 2]]Zip<, > // [[1, 3], [2, 4]]
Repeat<A, N>
Returns a static array of length N
with A
in each position.
Repeat<0, _0> // []Repeat<0, _1> // [0]Repeat<0, _2> // [0, 0]Repeat<0, _3> // [0, 0, 0]
Length<A>
Returns the length of a static array.
Length<> // 0Length<> // 1>Length<> // 2Length<> // 3 ;;Length<Repeat<0, _20>> // 20
LengthN<A>
Same as Length
but returns a Nat
instead.
LengthN<> // _0LengthN<> // _1LengthN<> // _2LengthN<> // _3LengthN<Repeat<0, _20>> // _20
Push<A, B>
Adds a type to the end of a static array.
Push<1, > // [1]Push<1, Cons<2, >> // [2, 1]Push<1, > // [3, 2, 1]
Map<A, B> and Reduce<A, B>
Type-level version of this operations.
declare Map<, "AsNat"> // [_0, _1, _2]Reduce< "Add", _0> // _6