$ npm install typescript-simple
Simple API to compile TypeScript code string to JavaScript. That's all!
typescript-simple
provides just one method that accepts TypeScript code string and returns JavaScript code.
Note: typescript-simple updates the major version for TypeScript's minor update including breaking changes.
$ npm install typescript-simple
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 CompilerOptions at 2nd argument.
var js = tss('var n: number = 1;', {noImplicitAny: true});
tss()
with options is not best-performance-method to be executed many times.
Use TypeScriptSimple
class for this purpose.
var TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
var tss = new TypeScriptSimple({target: ts.ScriptTarget.ES6, noImplicitAny: true});
var js1 = tss.compile('var n: number = 1;');
var js2 = tss.compile('var s: string = "foo";');
If you don't need TypeScript semantic error and just want the result code, give 2nd argument of the constructor false
.
var tss = new TypeScriptSimple({target: ts.ScriptTarget.ES6}, false);
var js = tss.compile('var n: string = 1;'); // an error is not thrown.
Note: syntactic errors may be thrown.
--jsx=preserve
var jsx = tss.compile('var foo: any = <Foo />;', {jsx: ts.JsxEmit.Preserve});
console.log(jsx); // 'var foo = <Foo />;'
--jsx=react
var tss = new TypeScriptSimple({jsx: ts.JsxEmit.React}, false);
var js = tss.compile('var foo: any = <Foo />;');
console.log(js); // 'var foo = React.createElement("Foo", null);'
Note: Ignore semantic errors if you use JsxEmit.React
.
Inline source map is available.
var tss = new TypeScriptSimple({sourceMap: true});
var js = tss.compile('var n: number = 1;', 'path/to/file.ts');
Output:
var n = 1;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uI...
Note: The path to file doesn't need to be an actual file. We just copy the contents of the passed in ts string into the inline sourceMap and make it look like the js is coming from a ts file at that file path.
typescript-simple
cannot compile multiple source files.
See index.d.ts.
MIT License: Teppei Sato <teppeis@gmail.com>
© 2010 - cnpmjs.org x YWFE | Home | YWFE