What makes a typescript module, a typescript module? Toastr example

2.2k views Asked by At

I was just using toastrjs for a few notifications, and I ran into this little problem. Ideally, when you import a library in nodejs, you have to make an import statement, like so:

import http = require("http");

However, when I tried this with toastr, I get an error, even after including the reference path. So, something like this:

///<reference path='toastr.d.ts' />
import toastr = require("./toastr");

I get this error:

error TS2071: Unable to resolve external module '"./toastr.js"'.
error TS2072: Module cannot be aliased to a non-module type.    

How is toastr different from a regular node module like http?

Update 1

I tried to do the same thing with jQuery but I have the same problems, does this mean that this does not work with frameworks that are designed to be client-side?

1

There are 1 answers

1
basarat On BEST ANSWER

the following declare definition would create a module you can import via amd/commonjs:

declare module "jquery"{
    export var jQuery: JQueryStatic;
}

Then you can do:

import jquery = require("jquery");

You can see such definitions in this underscore definition: https://github.com/borisyankov/DefinitelyTyped/blob/master/underscore/underscore.d.ts#L2853

or node.d.ts : https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node.d.ts#L203

However not all files on DT have this definition. As it is simple enough to add on your own and you are free to name these modules whatever you want (in your AMD configuration http://www.youtube.com/watch?v=4AGQpv0MKsA )