npm i ts2c


Convert Javascript/TypeScript to C

by Andrei Markeev

2.2.9 (see all)License:ISCTypeScript:Not Found
npm i ts2c

JavaScript/TypeScript to C transpiler

Produces readable C89 code from JS/TS code.

For example, this JavaScript:

console.log("Hello world!");

transpiles to the following C code:

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;

No excessive code that is not actually needed is ever generated.

The output is as readable as possible and mostly maps well to the original code.

Another example:

var obj = { key: "hello" };
obj["newKey"] = "test";

transpiles to the following C code:

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

struct obj_t {
    const char * key;
    const char * newKey;

static struct obj_t * obj;
int main(void) {

    obj = malloc(sizeof(*obj));
    assert(obj != NULL);
    obj->key = "hello";
    obj->newKey = "test";

    printf("{ ");
    printf("key: \"%s\"", obj->key);
    printf(", ");
    printf("newKey: \"%s\"", obj->newKey);
    printf(" }\n");


    return 0;

Project status

Work in progress: it works, but only about 70% of ES3 specification is currently supported: statements and expressions - 95%, built-in objects - 17%.

Notable NOT supported features include, for example: float and big numbers (all numbers are int16_t currently), eval, Date, Math, etc.

Detailed information about supported and planned features can be found in COVERAGE.md.

Contributions are welcome! See src/README.md

Live demo

You can try it out yourself online:


The main motivation behind this project was to solve problem that IoT and wearables cannot be currently efficiently programmed with JavaScript.

The thing is, for sustainable IoT devices that can work for a long time on single battery, things like Raspberry Pi won't do. You'll have to use low-power microcontrollers, which usually have very little memory available.

RAM ranges literally from 512 bytes to 120KB, and ROM/Flash from 1KB to 4MB. In such conditions, even optimized JS interpreters like JerryScript, Espruino or V7 are sometimes too much of an overhead and usually lead to the increased battery drain and/or don't leave a lot of system resources to your program.

Of course, transpiler cannot map 100% of the JavaScript language and some things are have to be left out, notably eval. Still, current conclusion is, that it is possible to transpile most of the language.


Planned transpilation targets:


Command line:

npm install -g ts2c


ts2c <files to transpile>


npm install ts2c
const ts2c = require("ts2c");
const cCode = ts2c.transpile("console.log('Hello world!')");

In browser:

<script src="https://unpkg.com/typescript"></script>
<script src="ts2c.bundle.js"></script>
    var cCode = ts2c.transpile("console.log('Hello world!')");
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

No reviews found
Be the first to rate