A package containing facilities for metaprogramming in typescript
This package is mainly built to tweak with the Typescript's typechecker.
Don't be hesitate to challenge yourself and contribute some "predicate"s to this repo here: https://github.com/Huy-DNA/meta-ts.
Post an issues on the package's repo!
npm install @huy-dna/meta-ts
The main checker is the two (dummy) functions checkAll
and check
:
checkAll<[
Predicate,
Predicate,
Predicate,
]>
and
check<Predicate>
Basically, you can imagine these two as functions that accepts arguments in angle brackets <>
and do not impose runtime overhead at all, as these will be checked as compile-time only.
From this section on, if a predicate P
is said to be "true", it means that check<P>
pass the Typescript typechecker.
Currently, these predicates are supported:
This predicate is true if T
and S
are the same type.
Example:
check<SameType<number, number>> // passed!
check<SameType<number, string>> // failed!
This predicate is true if T
extends S
.
Example:
check<DoesExtend<number, string>> // failed!
check<DoesExtend<3, number>> // passed!
This predicate is true if enum T
and enum S
have the same keys with the same numerical value.
Example:
enum E1 {
A,
B,
C
}
enum E2 {
A = 0,
B,
C = 2,
}
check<SameNumericalEnum<E1, E2>> // passed!
Note that this only works with number-valued enums!
- May not work correctly with
any
.