This project is read-only.

Allow 'injected' JavaScript in my TypeScript generated code!

Topics: General
Oct 18, 2012 at 12:04 PM
Edited Oct 18, 2012 at 12:14 PM

I would like suggest to be able to 'inject' verbatim plain old JavaScript into the code generated from my TypeScript source file.

This could be done by surrounding the JavaScript to be injected with a commented region much like regions work. i.e. basically this would be code that is simply passed through the compiler and injected into the generated code at the same point as in the source. e.g.

class MyTypescriptClass {
    name: string;
    constructor(name: string) { = name;
    // emit all content between #javascript and #endjavascript
    // at this point in the target jsvascript output and don't
    // do any TypeScript checking...
    this.myFunc = function() {
        // do whatever...

Oct 18, 2012 at 2:10 PM

I do not understand why you would need to disable typescript in your example, but I agree it would be nice to be able to do so sometimes, for example to use reserved keywords.


define(function (require) {

    module("some tests");

    test("foo should return 199", ()=> { 


Oct 18, 2012 at 3:09 PM


My example is crap because it does nothing; but it does express the idea of being able to pass raw JavaScript through the compiler and inject it into the generated code at the point it appears in the .ts file.  

This capability would then allow us to live in a TypeScript and raw JavaScript world all in the same file (and do lots of magic)!  I guess some of the more practical uses would be to utilise/inject existing JavaScript code without the need for definition files.  Yes this detracts from the type-safe world that we love from TypeScript but like many others coming from a Java/C# (or more formal languages) world we also love the unsafe world of JavaScript...

Oct 18, 2012 at 3:44 PM

Ok I see, 

usually I just declare the undefined stuff as any in those cases.

declare var someNiceLibrary: any;
someNiceLibrary.what.ever = "123";

Oct 18, 2012 at 7:21 PM

Yes... All you should ever need to do is cast to <any> (or don't just declare a type for things.)  I've posted serveral example bits of code to the forums where I'm adding methods to a class after the fact using its prototype, binding a classes methods to its 'this' pointer dynamically at runtime, etc.  The tricky bit is getting TypeScript to understand this magical thing you did for intelisense purposes.  But even then it usually just comes down to using declares & interfaces.


Oct 19, 2012 at 6:36 AM

@ickman, yes you can create TypeScript ambients until the cows come home but that misses what I'm suggesting.

What I'm suggesting is to be able to mix both TypeScript and JavaScript in the same file via a scoped region.  The raw JavaScript would be ignored by intellisense.   This would be similar to what we were able to do in 'C' with the 'asm' keyword.  In 'C' the block of code (assembler) within the braces was passed directly to output by the compiler.

I'm suggesting something similar to 'C's 'asm' keyword for passing JavaScript right through the TypeScript compiler.

Oct 19, 2012 at 7:37 AM

I get what you're asking for but TypeScript IS JavaScript so you should be able to type vanilla JavaScript without the compiler caring... Granted there are some exceptions to this rule, for instance the code below would yield an error due to type inferance:

var x = 'foo';
x = 7;

But that's a pretty simple fix if type 'x' as an <any>:

var x: any = 'foo';
x = 7;

So with the exception of type inferance issues like above you should be able to type vanilla JavaScript without any compiler errors.  I guess there may be some value in using a mechanisim like you suggest to turn off the type inferance for a block of code.  I will say that I've been programming in TypeScript for over 6 months now and I jump back and forth between TypeScript & JavaScript all the time and really haven't found this to be much of an issue.  Every once in a while you need the odd <any> cast but it's really not too bad... Others may have a heavier need to be dynamically typed so I'm willing to conceed that this feature might be really useful to some.