GithubHelp home page GithubHelp logo

happy-ferret / typescript-simple Goto Github PK

View Code? Open in Web Editor NEW

This project forked from teppeis/typescript-simple

0.0 1.0 0.0 145 KB

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

JavaScript 53.99% TypeScript 46.01%

typescript-simple's Introduction

typescript-simple

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

npm version npm downloads Node.js Version Support build status windows build status dependency status License

Description

typescript-simple provides just one method that accepts TypeScript code string and returns JavaScript code.

Why?

  • TypeScript v1.4 doesn't have simple TS string to JS string API
  • TypeScript v1.5 has ts.transpile(), but it cannot generate source map
  • TypeScript v1.6+ has ts.transpile() with source map, but it always ignores type checking

Versioning

  • typescript-simple v8.x.x uses TypeScript v2.2
  • typescript-simple v7.x.x uses TypeScript v2.1
  • typescript-simple v6.x.x uses TypeScript v2.0
  • typescript-simple v5.x.x uses TypeScript v1.8
  • typescript-simple v4.x.x uses TypeScript v1.7
  • typescript-simple v3.x.x uses TypeScript v1.6
  • typescript-simple v2.x.x uses TypeScript v1.5
  • typescript-simple v1.x.x uses TypeScript v1.4

Note: typescript-simple updates the major version for TypeScript's minor update including breaking changes.

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'.
}

Compiler Options

Specify CompilerOptions at 2nd argument.

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

Make repeated compilation faster

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";');

Ignore semantic errors

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 (.tsx)

  • --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.

Source map

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.

Limitations

typescript-simple cannot compile multiple source files.

API

See index.d.ts.

Contributors

License

MIT License: Teppei Sato <[email protected]>

typescript-simple's People

Contributors

9renpoto avatar aereal avatar basarat avatar conquestarrow avatar greenkeeperio-bot avatar igorminar avatar kkpoon avatar quramy avatar teppeis avatar vojtechhabarta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.