typescript-transform-macros
Typescript Transform Macros
Last updated 2 years ago by danielpanpm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install typescript-transform-macros 
SYNC missed versions from official npm registry.

typescript-transform-macros

Typescript Transform Macros

Build Status Maintainability codecov

npm version code style: prettier Conventional Commits Built with Spacemacs

Examples from https://github.com/codemix/babel-plugin-macros.

Installation

npm install --save-dev typescript-transform-macros

Usage with ttypescript

Add it to plugins in your tsconfig.json

{
  "compilerOptions": {
    "plugins": [{ "transform": "typescript-transform-macros" }]
  }
}

Also declare globally the MACRO function:

declare function MACRO<T>(t: T): T;

Example

Input:

declare function MACRO<T>(t: T): T;

const MAP = MACRO(
  <T, L>(
    inputConst: T[],
    visitor: (value: T, index?: number, input?: T[]) => L
  ) => {
    const input = inputConst;
    const length = input.length;
    const result = new Array(length) as L[];
    for (let i = 0; i < length; i++) {
      result[i] = visitor(input[i], i, input);
    }
    return result;
  }
);

declare interface Array<T> {
  MAP: Array<T>["map"];
}

console.log([1, 2, 3].MAP(n => 3 * n + 1));

Output:

"use strict";
const input_1 = [1, 2, 3];
const length_1 = input_1.length;
const result_1 = new Array(length_1);
for (let i_1 = 0; i_1 < length_1; i_1++) {
  result_1[i_1] = 3 * input_1[i_1] + 1;
}
console.log(result_1);
//> [ 4, 7, 10 ]

Current Tags

  • 1.1.1                                ...           latest (2 years ago)

3 Versions

  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 0
Last Month 4
Dependencies (0)
None
Dev Dependencies (4)

Copyright 2014 - 2021 © npmmirror.com |