npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@autumnblaze/typescript-simple

v9.0.0

Published

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

Downloads

3

Readme

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 - v2.9
  • 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]>