How to import js library without definition file into typescript file

I want to switch from JavaScript to TypeScript to help with code management as our project gets bigger. However, we use many libraries like amd Modules, which we do not want to convert to TypeScript.

We still want to import them into TypeScript files, but we also do not want to generate definition files. How can we achieve this?

eg. New TypeScript file:

/// <reference path="../../../../definetelyTyped/jquery.d.ts" /> /// <reference path="../../../../definetelyTyped/require.d.ts" /> import $ = require('jquery'); import alert = require('lib/errorInfoHandler'); 

Here lib/errorInfoHandler is an amd module included in a huge JavaScript library that we don't want to touch on.

Using the code above, the following errors occur:

 Unable to resolve external module ''lib/errorInfoHandler'' Module cannot be aliased to a non-module type. 

This really should lead to the following code:

 define(["require", "exports", "jquery", "lib/errorInfoHandler"], function(require, exports, $, alert) { ... } 

Is there a way to import a JavaScript library into TypeScript as an amd module and use it inside a TypeScript file without creating a definition file?

+51
javascript requirejs typescript
Apr 03 '14 at 15:33
source share
3 answers

The combination of the two answers given here worked for me.

 //errorInfoHandler.d.ts declare module "lib/errorInfoHandler" { var noTypeInfoYet: any; // any var name here really export = noTypeInfoYet; } 

I'm still new to TypeScript, but it seems like it's just a way to tell TypeScript to omit by exporting a dummy variable without type information.

EDIT

In the comments on this answer, it was noted that you can achieve the same result by simply declaring:

 //errorInfoHandler.d.ts declare module "*"; 

See github comment here .

+20
Jan 13 '15 at 4:59
source share

Create your own definition file with the following contents:

 declare module "lib/errorInfoHandler" {} 

And refer to this file in which you want to use import.

Or add the following line to the top of the file:

 /// <amd-dependency path="lib/errorInfoHandler"> 

Note. I don’t know if the latter works, as I originally worked with AMD's missing dependencies. Also note that with this approach you will not have IntelliSense for this file.

+4
Apr 04 '14 at 9:26
source share

Create a file in lib called errorInfoHandler.d.ts . Write there:

 var noTypeInfoYet: any; // any var name here really export = noTypeInfoYet; 

Now import alert will be successful and will be of type any .

+1
Apr 03 '14 at 19:14
source share



All Articles