Registry / devops / ts2c
library3.0.0jsnpmunverified

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.

npm install ts2c
INSTALL
IMPORT
SIG · TS2C
T
ts2c
devopsjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transpile
import { transpile } from 'ts2c'
const ts2c = require('ts2c'); ts2c.transpile(...)
ESM and CJS both supported; named export as of v3.
transpile
const { transpile } = require('ts2c')
const ts2c = require('ts2c').default
CJS destructured import works; no default export.
ts2c
<script src="ts2c.bundle.js"></script> const cCode = ts2c.transpile(...)
import { transpile } from 'ts2c' in browser without bundler
Browser bundle exposes global ts2c object; requires TypeScript script tag for type checking.

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);
ts2c --version
Debug
Known issues
breakingAll 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.
affects: >=3.0.0
gotchaBuilt-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.
affects: >=3.0.0
gotchaeval() 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.
affects: >=3.0.0
deprecatedNode.js API transpile() returns string; no streaming or file output built-in.
fix
Write result to file manually: fs.writeFileSync('out.c', transpile(code));
affects: >=3.0.0
Errors
Common errors & fixes
Error: TypeScript compiler not found. Ensure TypeScript is installed globally or in node_modules.
Missing TypeScript peer dependency when using CLI.
fix
Run: npm install -g typescript
TypeError: ts2c.transpile is not a function
Incorrect import: using default import or missing named export.
fix
Use: import { transpile } from 'ts2c' or const { transpile } = require('ts2c')
Unsupported built-in: 'Math.sin'
Math object methods are not implemented.
fix
Replace with manual implementation or avoid trigonometric functions.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
typescriptoptionalRequired for parsing TypeScript/JavaScript syntax in browser build; CLI uses native TypeScript API.
Agent activity
4 hits · last 30 days
node
4
Resources
ts2c — npm install ts2c · libregistry