1

Is it possible to use TypeScript and jQuery (or any other non-Node JavaScript library) without AMD?

Currently my code compiles fine, but the compiled code cannot see the jQuery's $ function.

Given this TypeScript code:

/// <reference path="typings/tsd.d.ts" />
import $ = require('jquery');

export class Widget {
  foo() { 
    $(selector).bar()
  }
}

I'd like the following JavaScript:

var Widget = (function ($) {
  function Widget() { }
  Widget.prototype.foo = function () { ... };
  return Widget;
})(jQuery);

Currently my code does not have jQuery passed to the anonymous function.

UPDATE 1

Per @basarat

Do it by hand. If its valid JavaScript then its valid TypeScript

You're suggesting (and I was thinking) that I do this:

var Widget = (function($) {
  class Widget {
    foo() {
      $('body').append('foo');
    }
  }
  return Widget;
})(jQuery);

?

That's not valid TypeScript and without that, I'm left with prototypes.

UPDATE 2

Per @basarat the above is supported with TypeScript 1.6.

1
  • I know I can do this by hand. I want to know if there's a TypeScript way to do this. Commented Sep 20, 2015 at 23:20

1 Answer 1

2

Given this TypeScript code , I'd like the following JavaScript

This is not how the TypeScript js emit is designed to work.

So in short : no. You can't get this javascript from that typescript.

I know I can do this by hand. I want to know if there's a TypeScript way to do this.

Do it by hand. If its valid JavaScript then its valid TypeScript. There isn't a specific TypeScript way to do this thing. The JavaScript way is the TypeScript way in this case.

Update

That's not valid TypeScript and without that, I'm left with prototypes.

It is valid TypeScript starting with TypeScript 1.6.

Sign up to request clarification or add additional context in comments.

4 Comments

Checkout the update to my question. Looks like to do it by hand I loose class, interface, etc... keywords
See update. It should work starting with typescript 1.6
Yup, 1.6 works. Do you know if this was added to 1.6 to support this problem?
This as well as others. More important was anonymous classes (e.g. class expressions) and inner classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.