Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Typescript how do I use a Javascript module when importing from Typeings

Using mgechev's angular2-seed, I'm trying to get to grips with Angular2 and Typescript for a new project, but have run into this problem.

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

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

So far, so good. Everything seems to transpile ok. 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? Have I missed a step out to tell Typescript where the actual code is?

like image 891
jeffeld Avatar asked May 31 '16 08:05

jeffeld


2 Answers

Typically what you want to do is, if the package is not a native Typescript module:

import * as numeral from 'numeral';

The typings folder is just for telling Typescript what the type definitions are, so it will use it for code highlighting and linting. The actual module you want to import sits in the node_modules folder and can be imported with its name.

If it's still complaining about not finding the numeral module you could add

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

or wherever the Typescript definition file is stored.

like image 132
rinukkusu Avatar answered Sep 27 '22 18:09

rinukkusu


you need also to add a reference link to actual numeraljs.js file. in your index.html page

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

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

also you can use system.js to load all needed scripts

like image 22
Tawfiq abu Halawah Avatar answered Sep 27 '22 17:09

Tawfiq abu Halawah