This project is read-only.

How to use the Compiler in JavaScript?

Topics: General
Sep 2, 2013 at 12:35 AM
I am trying to compile source code on the fly from JavaScript.
I have it working with an older version of TypeScript.

But the API have changed a lot in the latest version and there is suddenly no longer a setErrorCallback() method and the addUnit() method no longer takes two parameters (source and name).

I have been trying to find the code used for the playground, but couldn't seem to find it.

Can anyone give me an example of how to use the latest version of the TypeScript compiler with JavaScript?

Or maybe guide me to where I can find the code used for compiling in the playground.

Sep 3, 2013 at 12:15 AM
lusbuab: Thanks, but I am not sure how that will work? There is no call to the TypeScriptCompiler ?

What I am currently doing is something similar to this

It works fine with an older version of the TypeScript Compiler, but the new one have a different API and etc. there is no longer a addUnit method that take 2 or 3 parameters, and other methods have been removed aswell.

I can't seem to find any example for the new version and the documentation for the API is non-existing.
Sep 5, 2013 at 11:11 PM
Thanks for trying to help. :)

But I still don't get how to use it to compile TypeScript source to JavaScript source.
I don't want to write a language service.
I just want to use the compiler to compile stuff in memory (like in the gist i posted)

There is just no examples or documentation to be found anywhere for the latest version.
Sep 6, 2013 at 8:03 PM
The test automation is one example of using the compiler APIs. You can find it in src\harness\harness.ts and its used in tests\runners\compiler\runner.ts.

addUnit is now addSourceUnit and takes the contents in the form of a ScriptSnapshot (which you can generate from a string via TypeScript.ScriptSnapshot.fromString).

I don't recall setErrorCallback offhand but you can see how we use the error reporting APIs in HarnessCompiler.reportCompilationErrors in the aforementioned harness.ts.