Skip to content

Just compile TypeScript code string to JavaScript. That's all!

Notifications You must be signed in to change notification settings

basarat/typescript-simple

 
 

Repository files navigation

typescript-simple NPM version build status Dependency Status

Just compile TypeScript code string to JavaScript. That's all!

Description

TypeScirpt 1.4 has official compiler API, but it isn't easy to compile tiny TypeScript code string. typescript-simple provides just one method that accepts TypeScript code string and returns JavaScript code.

Install

$ npm install typescript-simple

Usage

Simple usage (default target is ES5).

var tss = require('typescript-simple');
var js = tss('var n: number = 1;');
console.log(js); // 'var n = 1;'

If the code causes errors, typescript-simple throws errors.

try {
    var js = tss('var n: number = "str";');
} catch (e) {
    console.error(e); // Error: L1: Type 'string' is not assignable to type 'number'.
}

Specify compiler options.

var js = tss('var n: number = 1;', {noImplicitAny: true});

But tss() with options is not best-performance-method to be called many times. Use TypeScriptSimple class for the purpose.

var tss = new tss.TypeScriptSimple({target: ts.ScriptTarget.ES6, noImplicitAny: true});
var js1 = tss.compile('var n: number = 1;');
var js2 = tss.compile('var s: string = "foo";');

Limitaions

typescript-simple cannot compile multiple source files.

API

See index.d.ts.

Note

This implementation is derived from code in an official wiki page.

License

MIT License: Teppei Sato <teppeis@gmail.com>

About

Just compile TypeScript code string to JavaScript. That's all!

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 63.3%
  • TypeScript 36.7%