Alexey Alexey - 3 months ago 99
TypeScript Question

Angular2-Webpack-Typescript - 3rd party libraries

I've started with a beautiful seed project: https://github.com/AngularClass/angular2-webpack-starter

And I've stuck with using 3rd-party modules.Can someone help/explain how to properly add/use external modules to this project?

For example, I'm adding 'immutable' using:

npm install immutable --save


After that, I'm trying to link that module inside a component:

import * as immutable from 'immutable';
import {List} from 'immutable';
import Immutable = require('immutable');


Nothing helps, it always returns TS2307: Cannot find module '
immutable
'. The same for '
moment
' for example.

Answer

There is a typings file you need to add to your project that is located in node_modules\immutable\dist\immutable.d.ts. You need to reference that in your project. It should look something like this:

/// <reference path="node_modules/immutable/dist/immutable.d.ts" />
import * as immutable from 'immutable';
import {List} from 'immutable';
import Immutable = require('immutable');

For moment there are typings available at DefinitelyTyped. I recommend using tsd to install them.

If you come across a library you would like to use that you can't find any typings for, you can see this question for an easy way to still use it. You just won't get any of the Typescript benefits.