Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best solution for importing non-typescript npm modules

with ES6 using traceur and SystemJS this form is correct:

import _ from 'lodash';

for Typescript it is not enough - I get error error TS2307: Cannot find module 'lodash' So, I install 'lodash.d.ts' :

/// <reference path="lodash/lodash.d.ts" />
import _ from 'lodash';

Now, I get: Module '"lodash"' has no default export. from Typescript compiler

So, I try 'node style':

/// <reference path="lodash/lodash.d.ts" />
let _ = require('lodash');

I get: Uncaught (in promise) Error: require is not a function in browser

Finally:

import _ = require('lodash');

and it works but it's 'old form' not proper ES6.

Is there a single, proper way to use ES6 style Typescript import for non-typescript modules?

(Typescript 1.6.2)

like image 798
Yoorek Avatar asked Oct 09 '15 07:10

Yoorek


2 Answers

Try import * as _ from 'lodash';

like image 110
bekos Avatar answered Nov 15 '22 19:11

bekos


if you import lodash as node_module, you might need to do it as declare var _: any;

if you wanna do import * as _ from 'lodash', then import loadash thru TypeScript Definition manager (tsd). in terminal, do it as

tsd install lodash --save

like image 22
mat Avatar answered Nov 15 '22 18:11

mat