【发布时间】:2016-06-08 01:01:15
【问题描述】:
我正在尝试了解如何正确使用 TypeScript 和经典 JS 节点模块。
我建立了一个非常基础的项目,文件架构如下:
.
├── index.ts
├── lodash.d.ts
├── module.ts
└── node_modules
└── lodash
lodash 已与npm 一起安装。由于它似乎没有提供任何类型信息,我写了一个基本的d.ts 文件,它只描述了一个功能,只是为了取悦tsc 并避免不知道lodash 的错误。
lodash.d.ts
declare module "lodash" {
export function reverse(array: any[]): any[];
}
在我的module.ts 文件中,我使用require 导入lodash,并在模块上公开一个函数,我在index.ts 文件上使用该函数。
module.ts
/// <reference path="./lodash.d.ts" />
import _ = require('lodash');
module FooModule {
export function foo() {
return _.reverse([1, 2, 3]);
}
}
index.ts
/// <reference path="./module.ts" />
let a = FooModule.foo();
console.log(a);
问题是tsc(以及VS Code)告诉我它找不到名称FooModule。
$ tsc index.ts --module commonjs -outDir build
index.ts(3,9): error TS2304: Cannot find name 'FooModule'.
但是,如果我从module.ts 中删除import _ = require('lodash');,它可以正常工作(除了_ 变量现在未定义的明显事实)。
我对这个require 做错了吗?
【问题讨论】: