Skip to content

Markers

GoogleFeud edited this page Aug 23, 2023 · 1 revision

Markers make macro parameters behave differently. They don't alter the parameter's type, but it's behaviour.

Accumulator

A parameter which increments every time the macro is called. You can only have one accumulator parameter per macro.

import { Accumulator } from "ts-macros"

function $num(acc: Accumulator = 0) : Array<number> {
    acc;
}
$num!();
$num!();
$num!();
0
1
2

Save

Saves the provided expression in a hygienic variable. This guarantees that the parameter will expand to an identifier. The declaration statement is also not considered part of the expanded code.

function $map(arr: Save<Array<number>>, cb: Function) : Array<number> {
    const res = [];
    for (let i=0; i < arr.length; i++) {
        res.push($$inline!(cb, [arr[i]]));
    }
    return $$ident!("res");
}
{
    const mapResult = $map!([1, 2, 3, 4, 5], (n) => console.log(n));
}
{
    let arr_1 = [1, 2, 3, 4, 5];
    const mapResult = (() => {
        const res = [];
        for (let i = 0; i < arr_1.length; i++) {
            res.push(console.log(arr_1[i]));
        }
        return res;
    })();
}
Clone this wiki locally