{"id":26518,"library":"ts2c","title":"ts2c: TypeScript/JavaScript to C Transpiler","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/andrei-markeev/ts2c","tags":["javascript"],"install":[{"cmd":"npm install ts2c","lang":"bash","label":"npm"},{"cmd":"yarn add ts2c","lang":"bash","label":"yarn"},{"cmd":"pnpm add ts2c","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for parsing TypeScript/JavaScript syntax in browser build; CLI uses native TypeScript API.","package":"typescript","optional":true}],"imports":[{"note":"ESM and CJS both supported; named export as of v3.","wrong":"const ts2c = require('ts2c'); ts2c.transpile(...)","symbol":"transpile","correct":"import { transpile } from 'ts2c'"},{"note":"CJS destructured import works; no default export.","wrong":"const ts2c = require('ts2c').default","symbol":"transpile","correct":"const { transpile } = require('ts2c')"},{"note":"Browser bundle exposes global ts2c object; requires TypeScript script tag for type checking.","wrong":"import { transpile } from 'ts2c' in browser without bundler","symbol":"ts2c","correct":"<script src=\"ts2c.bundle.js\"></script>\nconst cCode = ts2c.transpile(...)"}],"quickstart":{"code":"import { transpile } from 'ts2c';\nconst cCode = transpile(`\nfunction factorial(n: number): number {\n  if (n <= 1) return 1;\n  return n * factorial(n - 1);\n}\nconsole.log(factorial(5));\n`);\nconsole.log(cCode);","lang":"typescript","description":"Transpile a recursive factorial function and print C code."},"warnings":[{"fix":"Use only integer arithmetic within int16 range; avoid fractions and large constants.","message":"All numbers are int16_t (range -32768 to 32767). Floating point and large numbers not supported.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Implement Date/Math functions manually or use alternative libraries; avoid prototype chaining.","message":"Built-in objects like Date, Math, JSON, and prototype-based inheritance are not transpiled.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Replace eval with static code; use computed property names only with known keys.","message":"eval() and dynamic property access with arbitrary strings may not work correctly.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Write result to file manually: fs.writeFileSync('out.c', transpile(code));","message":"Node.js API transpile() returns string; no streaming or file output built-in.","severity":"deprecated","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run: npm install -g typescript","cause":"Missing TypeScript peer dependency when using CLI.","error":"Error: TypeScript compiler not found. Ensure TypeScript is installed globally or in node_modules."},{"fix":"Use: import { transpile } from 'ts2c' or const { transpile } = require('ts2c')","cause":"Incorrect import: using default import or missing named export.","error":"TypeError: ts2c.transpile is not a function"},{"fix":"Replace with manual implementation or avoid trigonometric functions.","cause":"Math object methods are not implemented.","error":"Unsupported built-in: 'Math.sin'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}