ts2c: TypeScript/JavaScript to C Transpiler

raw JSON →
3.0.0 verified Fri May 01 auth: no javascript

ts2c transpiles JavaScript/TypeScript (ES3 subset) to readable C89 code for low-power microcontrollers (ESP32, ESP8266, AVR). Version 3.0.0 is under active development, supporting ~70% of ES3 (95% statements/expressions, 17% built-in objects). All numbers are int16_t; no floats, big numbers, prototype, eval, Date, or Math. Output is minimal, self-contained C with no runtime overhead. Different from JerryScript/Espruino by compiling to native C rather than interpreting, reducing memory and battery usage.

error Error: TypeScript compiler not found. Ensure TypeScript is installed globally or in node_modules.
cause Missing TypeScript peer dependency when using CLI.
fix
Run: npm install -g typescript
error TypeError: ts2c.transpile is not a function
cause Incorrect import: using default import or missing named export.
fix
Use: import { transpile } from 'ts2c' or const { transpile } = require('ts2c')
error Unsupported built-in: 'Math.sin'
cause Math object methods are not implemented.
fix
Replace with manual implementation or avoid trigonometric functions.
breaking All numbers are int16_t (range -32768 to 32767). Floating point and large numbers not supported.
fix Use only integer arithmetic within int16 range; avoid fractions and large constants.
gotcha Built-in objects like Date, Math, JSON, and prototype-based inheritance are not transpiled.
fix Implement Date/Math functions manually or use alternative libraries; avoid prototype chaining.
gotcha eval() and dynamic property access with arbitrary strings may not work correctly.
fix Replace eval with static code; use computed property names only with known keys.
deprecated Node.js API transpile() returns string; no streaming or file output built-in.
fix Write result to file manually: fs.writeFileSync('out.c', transpile(code));
npm install ts2c
yarn add ts2c
pnpm add ts2c

Transpile a recursive factorial function and print C code.

import { transpile } from 'ts2c';
const cCode = transpile(`
function factorial(n: number): number {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}
console.log(factorial(5));
`);
console.log(cCode);