Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 481 Bytes

comptimeEval.md

File metadata and controls

28 lines (21 loc) · 481 Bytes

tsts/comptimeEval

Evalute code on compilation.
Macro-like functionality

Source:

declare function __compilerEval<T>(x: string): any;

function comptimeStructs() {
    type Variants = 'a' | 'b' | 'c' | 'e';

    __compilerEval<Variants>(`
        let out = '';
        $eachOfStrUnion(x => out += x + ': 0, ');
        return 'const Struct = {' + out + '}';
    `);
}

Compiled:

function comptimeStructs() {
    var Struct = { a: 0, b: 0, c: 0, e: 0, };
}