Typescript导入带有ECMAscript 6语法的node_modules

use*_*232 3 javascript typescript

我从npm 安装了库lodash,现在我想将它导入我的文件,如下所示:

import _ from 'lodash';
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

错误TS1192:模块'"lodash"'没有默认导出.

为什么我会收到此错误?以及如何使用ECMAscript6的新导入语法导入不是.ts文件的node_modules

Mar*_*cka 5

以下两种方法适用于我:

使用要求:

/**
 * Install package via
 *   $ bower install lodash --save
 * Run:
 *   $ node test.js  # after TypeScript compilation
 */

// test.ts file
/// <reference path="typings/lodash/lodash.d.ts" />
import _ = require('./bower_components/lodash/lodash.js');
console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
Run Code Online (Sandbox Code Playgroud)

ES6 导入:

/**
 * Install package via
 *   $ tsd install lodash --save # to download definition file
 *   $ npm install lodash --save
 * 
 * Run:
 *   $ node test.js  # after TypeScript compilation 
 */
// test.ts file
/// <reference path="typings/lodash/lodash.d.ts" />
import * as _ from 'lodash';

console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
// ? [['a', 'b'], ['c', 'd']]
Run Code Online (Sandbox Code Playgroud)

注意:基于路径映射的TypeScript 1.8计划的模块分辨率(https://github.com/Microsoft/TypeScript/issues/5039)