/meta-ts

A repo containing facilities for metaprogramming in typescript

Primary LanguageTypeScriptApache License 2.0Apache-2.0

meta-ts

npm Download

A package containing facilities for metaprogramming in typescript

Contribution

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.

Bug reports

Post an issues on the package's repo!

Installation

npm install @huy-dna/meta-ts

Usage

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.

Predicates

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:

SameType<T, S>

This predicate is true if T and S are the same type.

Example:

check<SameType<number, number>> // passed!
check<SameType<number, string>> // failed!

DoesExtend<T, S>

This predicate is true if T extends S.

Example:

check<DoesExtend<number, string>> // failed!
check<DoesExtend<3, number>> // passed!

SameNumericalEnum<T, S>

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!

Pitfalls

  • May not work correctly with any.