Search code examples
typescriptangularcloud9-ideangular2-services

Using javascript library from Typescript based Angular2 project


I'm trying to create a proof-of-concept app with Angular 2 and Typescript. I'm wanting to incorporate the toastr notification library and was hoping there would be a simple way to get it running without the definition files.

I've created a file in my common services directory called toastr.d.ts]:

  declare module "toastr" {
//   var noTypeInfoYet: any; 
//   export = noTypeInfoYet;
}

which is called from the service common.ts

 /// <reference path="toastr.d.ts" />
import {Injectable} from 'angular2/core';
import {toastr} from 'toastr';

@Injectable()
export class Common {

  constructor() {}
  
  log(message:string){
      toastr(message);
      console.log(message);
  }

}

however I get the compilation error: [DiffingTSCompiler]: Typescript found the following errors: app/services/common/common.ts (3,9): Module '"toastr"' has no exported member 'toastr'.

All of the code can be found on Cloud 9


Solution

  • The following:

    import {toastr} from 'toastr';
    

    is wrong. toastr doesn't export a variable toastr in fact the root level export IS toastr. Fix:

    import * as toastr from 'toastr';