Is there an equivalent of Julia's metaprogramming and macros for JavaScript and TypeScript?

Viewed 372
1 Answers

No, there is no equivalent. Neither ECMAScript nor TypeScript have macros.

There is a proposal for Decorators, which would allow you to do a tiny subset of what you can do with macros, but that proposal is still in Stage 2 of the specification process.

There are currently no official proposals to add Macros to ECMAScript (I went through the list of all Stage 3, Stage 2, Stage 1, and Stage 0 proposals) nor have there ever been (I also checked inactive and withdrawn proposals). There is however recurring discussion about it, not the least since at one point, some Scheme designers were also part of the ECMAScript committee.

What would be achieved using macros in languages like Julia, Scheme, Rust, Boo, Nemerle, and co. is usually solved with source-to-source compilers (sometimes called transpilers) in the ECMAScript community. The most popular of which is Babel. Babel is mostly used to "compile away" newer language features, i.e. allow a programmer to use the latest language features and even not-yet-released proposals, but still run the code in environments where only an older version of the language is available.

There are, however, some third-party macro libraries available that add macros to ECMAScript via … you guessed it … source-to-source transpilation. One of these is Sweet.js.

Related