typebolt
v0.7.0
Published
TypeScript static helpers
Downloads
136
Readme
TypeScript static helpers for types accuracy and testing
Install
yarn add --dev typebolt
Minimal TypeScript Version: 4.9
Content
Logical Operators
Not<T>
: Logical NotAnd<A, B>
: Logical AndOr<A, B>
: Logical OrXor<A, B>
: Logical Exclusive OrNor<A, B>
: Logical Not OrNand<A, B>
: Logical Not AndXnor<A, B>
: Logical Exclusive Not Or
Not<true> // false
Not<false> // true
And<true, true> // true
And<true, false> // false
And<true, false> // false
Or<true, false> // true
Or<true, true> // true
// ...
Not<Or<true, false>> // false – Equivalent to Nor
Not<And<true, false>> // true – Equivalent to Nand
Testing Types
IsType<T, S>
: S is a Subtype of T (a.k.a.extends
)IsUnion<T>
: T an UnionIsAny<T>
: T is anyIsExactType<T1, T2>
: T1 and T2 are exact same.
Assert
Assert a Type is true
or false
.
import { value Assert } from "typebolt";
Assert<true>();
Assert<false>(); // ERROR
Assert<boolean>(); // ERROR
Assert.True<true>();
Assert.True<false>(); // ERROR
Assert.True<boolean>(); // ERROR
Assert.False<false>();
Assert.False<true>(); // ERROR
Assert.False<boolean>(); // ERROR
IsType<T, S>
Check S is subtype of T. (a.k.a. extends
)
Assert<IsType<number, 42>>();
Assert<IsType<number, number>>();
Assert<IsType<42, number>>(); // ERROR
IsExactType<T1, T2>
Check T1 and T2 are exact same types.
Assert<IsExactType<42, 42>>();
Assert<IsExactType<any, 42>>(); // ERROR
Assert<IsExactType<number, 42>>(); // ERROR
// With unions
Assert<IsExactType<string | number, string | number>>();
Assert<IsExactType<string | number, string>>(); // ERROR
Assert<IsExactType<string, string | number>>(); // ERROR
// With any
Assert<IsExactType<any, any>>();
Assert<IsExactType<any, number>>(); // ERROR
Tuple Operations
Head<T>
: First element of TTail<T>
: All elements after head of TPrepend<X, T>
: Add element X in front of TAppend<X, T>
: Add element X at end of TReverse<T>
: Reverse TTake<N, T>
: Take N first elements of TTakeLast<N, T>
: Take N last elements of TDrop<N, T>
: Remove N first elements of TDropLast<N, T>
: Remove N last elements of T
Head<["Hello", "World"]> // "Hello"
Tail<["Hello", "World"]> // ["World"]
Prepend<1, [2, 3, 4]> // [1, 2, 3, 4]
Append<4, [1, 2, 3]> // [1, 2, 3, 4]
Reverse<[1, 2, 3, 4]> // [4, 3, 2, 1]
Take<2, [1, 2, 3, 4]> // [1, 2]
TakeLast<2, [1, 2, 3, 4]> // [3, 4]
Drop<2, [1, 2, 3, 4]> // [3, 4]
DropLast<2, [1, 2, 3, 4]> // [1, 2]
Caveat
boolean
is always considered union oftrue | false
.Union of a Type
T
and a SubTypeS
will resolve to non-unionT
.e.g.
string | "Hello"
resolves tostring
Union with
any
will always resolve toany
.