API
Contents
- Arithmetics
- Conditionals and comparisons
- Logics
- Tuples
Fill<N, T?>
construct a tuple of size NFirst<...T>
, Last<...T>
first and last tuple elementRest<...R>
, ReverseRest<...R>
all but the first / lastLargerThan<...T1, ...T2>
, SameLength<...T1, ...T2>
tuple size comparisonLengthOf<...T>
length of tupleOptional<...T1, ...T2>
, OptionalFrom<...T, N>
make optional tuple elementsOverwrite<...T1, ...T2>
overwrite one tuple with anotherReverse<...T>
reverse the types in a tupleShift<...T, N>
remove the first N elements in a tupleSlice<...T, Start?, End?>
slice a tupleTruncate<...T, N>
truncate a tupleUnionAll<...T1, T2>
union all tuple element types with a type
Arithmetics
The arithmetics in this library handles positive integers up to ~98, sometimes more.
Add
Add two positive numbers:
import type { Add } from 'meta-types'
type T = Add< 13, 11 >;
Sub
Subtract the second number from the first number:
import type { Sub } from 'meta-types'
type T = Sub< 13, 11 >;
Mul
Multiply two numbers:
import type { Mul } from 'meta-types'
type T = Mul< 13, 11 >;
Conditionals and comparisons
Conditional
Return Then if If otherwise Else, or; if If
then Then
else Else
.
import type { If, Extends } from 'meta-types'
type T1 = If< true, "yes", "no" >;
type T2 = If< false, "yes", "no" >;
type T3 = If< Extends< "42", number >, "yes", "no" >;
Extends
Returns true if T
extends E
, otherwise false. This is a trivial operation, but practical in meta functional statements, e.g. If
.
import type { Extends } from 'meta-types'
type T1 = Extends< true, boolean >;
type T1 = Extends< "42", number >;
GreaterThan
GreaterThan
returns true if the first number is greater than the second.
A third argument can be set to true
, to turn this into greater-than-or-equal.
import type { GreaterThan } from 'meta-types'
type T1 = GreaterThan< 42, 40 >;
type T2 = GreaterThan< 40, 40, true >;
type T3 = GreaterThan< 40, 42 >;
Is
Is
returns true for equal types.
import type { Is } from 'meta-types'
type T1 = Is< 42, number >;
type T2 = Is< null, undefined >;
type T3 = Is< "foo", "foo" >;
IsFalsy
IsFalsy
returns true for any falsy type (false
, ""
, 0
, null
and undefined
).
import type { IsFalsy } from 'meta-types'
type T1 = IsFalsy< 0 >;
type T2 = IsFalsy< 3 >;
Logics
Logic (and, or, xor)
And
, Or
and Xor
can be used to perform logical operations on booleans.
import type { And, Or, Xor } from 'meta-types'
type T1 = And< true, false >;
type T2 = Or< true, false >;
type T2 = Xor< true, false >;
Not
Invert true
or false
using Not
.
import type { Not } from 'meta-types'
type F = Not< true >;
type T = Not< false >;
A second argument Strict
can be provided, which defaults to true
. If this is set to false
, and falsy type will return true
instead of never
.
import type { Not } from 'meta-types'
type F = Not< 0 >;
type T = Not< 0, false >;
Tuples
Fill
Create a tuple of size N
, filled with any
(or type T
if provided).
import type { Fill } from 'meta-types'
type T1 = Fill< 3 >;
type T2 = Fill< 4, null >;
First, Last
First
and Last
can be used to extract the first and last elements in a tuple.
import type { First, Last } from 'meta-types'
type T = [ "hello", 42, "world", true ];
type T1 = First< T >;
type T2 = Last< T >;
Rest, ReverseRest
Rest
and ReverseRest
can be used to extract all-but-the-first or all-but-the-last elements in a tuple.
import type { Rest, ReverseRest } from 'meta-types'
type T = [ "hello", 42, "world", true ];
type T1 = Rest< T >;
type T2 = ReverseRest< T >;
LargerThan, SameLength
LargerThan
and SameLength
can be used to extract and compare the length of tuples. A second argument can be provided to LargerThan
to turn it into larger-than-or-same-length.
import type { LargerThan, SameLength } from 'meta-types'
type T1 = LargerThan< [ 0, 0 ], [ 0, 0, 0 ] >;
type T2 = SameLength< [ 0, 0 ], [ 0, 0 ] >;
type T3 = LargerThan< [ 0, 0 ], [ 0, 0 ], true >;
LengthOf
LengthOf
returns the length of a tuple, or a fallback type (defaults to -1
) if the tuple is unbounded, i.e. it's rest-spread like [ "foo", 42, ...null[] ]
.
import type { LengthOf } from 'meta-types'
type T1 = LengthOf< [ 0, 0 ] >;
type T2 = LengthOf< [ 0, 0, ...any ] >;
type T3 = LengthOf< [ 0, 0, ...any ], "ouch" >;
Optional
Append an optional tuple to a required tuple.
import type { Optional } from 'meta-types'
type T = Optional< [ string, number ], [ boolean, any ] >;
Turn all types after index N into optional.
import type { OptionalFrom } from 'meta-types'
type T = OptionalFrom< [ string, number, boolean, any ], 2 >;
Overwrite
Overwrite the first elements in a tuple, with elements from another tuple. The first tuple will be overwritten with types in the second tuple.
import type { Overwrite } from 'meta-types'
type T1 = Overwrite< [ 1, 2, 3 ], [ "a", "b" ] >;
type T2 = Overwrite< [ 1, 2 ], [ "a", "b", "c" ] >;
Reverse
Reverse a tuple.
import type { Reverse } from 'meta-types'
type T = Reverse< [ string, boolean, 42 ] >;
Shift
Shift (remove the first) N elements from a tuple.
import type { Shift } from 'meta-types'
type T = Shift< [ string, boolean, number, 42, "foo" ], 2 >;
Slice
Slice a tuple with a start and end offset.
import type { Slice } from 'meta-types'
type T = Slice< [ string, boolean, number, 42, "foo" ], 2, 4 >;
Truncate
Truncate a tuple to a max length.
import type { Truncate } from 'meta-types'
type T = Truncate< [ string, boolean, number, 42, "foo" ], 2 >;
Union
Union all the types in a tuple, with a type.
import type { UnionAll } from 'meta-types'
type T = UnionAll< [ string, boolean, number ], U >;