Amaro TypeScript Wrapper

1.1.8 · active · verified Sun Apr 19

Amaro is a low-level Node.js wrapper around `@swc/wasm-typescript`, a WebAssembly port of the SWC TypeScript parser. It primarily performs type stripping, which is utilized internally by Node.js for its native TypeScript support. As of its latest stable release, version 1.1.8, Amaro offers a stable API that allows developers to use a specific TypeScript transpiler version independently of the one embedded in Node.js. The package sees frequent updates, often aligning with new SWC versions. Its key differentiators include synchronous type stripping (`transformSync`), flexible loader mechanisms (`amaro/strip`, `amaro/transform`) for direct execution of TypeScript files, and enhanced monorepo development workflows via conditional exports, abstracting away build steps. It currently supports TypeScript version 5.8.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates synchronous type stripping using `amaro.transformSync` and provides examples of how to activate Amaro as a Node.js loader for direct TypeScript execution.

import amaro from 'amaro';

const tsCode = `
  interface User {
    id: number;
    name: string;
  }

  function greet(user: User): string {
    return \`Hello, \${user.name}!\`;
  }

  const myUser: User = { id: 1, name: 'Alice' };
  console.log(greet(myUser));
`;

console.log("Original TypeScript Code:");
console.log(tsCode);

// Use transformSync to strip types. 
// The 'strip-only' mode replaces type annotations with whitespace 
// to preserve original line numbers for stack traces.
const { code: strippedCode } = amaro.transformSync(tsCode, { mode: "strip-only" });

console.log("\nTransformed JavaScript Code (types stripped):\n---\n");
console.log(strippedCode);
console.log("\n---\n");

/*
// To use Amaro as a Node.js loader to execute TypeScript files directly:

// 1. Create a file named 'my-app.ts':
//    const message: string = "Hello from Amaro loader!";
//    console.log(message);

// 2. Run with Node.js using the --import flag (for type stripping):
//    node --import="amaro/strip" my-app.ts

// 3. For more advanced features (like decorators, class fields) and source maps:
//    node --enable-source-maps --import="amaro/transform" my-app.ts

// 4. For programmatic loader registration (e.g., in a bootstrap file):
//    // bootstrap.mjs
//    import { register } from "node:module";
//    register("amaro/strip", import.meta.url);
//    await import("./my-app.ts");
//    // Then run: node --watch ./bootstrap.mjs
*/

view raw JSON →