In Typescript, how can I use the Javascript module when importing from Typeings

Using mgechev angular2 -seed , I am trying to deal with Angular2 and Typescript for a new project, but have encountered this problem.

I want to use Numeral in the component, so I:

  • Installed digit using npm install numeral
  • Set typing for Numeral with typings install dt~numeraljs --global --save
  • In my added component import { numeral } from '/typings/globals/numeraljs';
  • Added a line of code: let num:Number = new Number(numeral().unformat(text));

So far so good. Everything seems to be in order. Until I get to the browser, where I get in the console:

 Error: XHR error (404 Not Found) loading http://localhost:5555/typings/globals/numeraljs.js(…) 

What am I doing wrong here? I skipped the step to tell Typescript where the actual code is?

+5
source share
2 answers

Usually you want the package to be a non-native Typescript module:

 import * as numeral from 'numeral'; 

The type folder is intended only to tell Typescript what type definitions are, so it will use it to highlight code and cast. The actual module you want to import is located in the node_modules folder and can be imported with its name.

If he still complains that he will not find the numeral module, you can add

 /// <reference path="typings/globals/numeraljs/numeraljs.d.ts"/> 

or where the Typescript definition file is stored.

+8
source

you also need to add a link link to the actual numeraljs.js file. at index.html page

say you have index.html page as your main page, add numeraljs.js to the head

 <head> <script src="myScriptLib/numeral/numeraljs.js"></script> </head> 

you can also use system.js to download all the necessary scripts

+1
source

All Articles