【发布时间】:2017-06-13 00:09:22
【问题描述】:
我想在 typescript 中使用简单的 commonjs 模块,这里有 3 个文件
原始库:
//commonjs-export-function.js
module.exports = function() {
return 'func';
};
定义文件:
//commonjs-export-function.d.ts
declare function func(): string;
export = func;
使用它的打字稿程序:
//main.ts
import { func } from './commonjs-function';
console.log(func());
当我运行 tsc 时出现此错误:
tsc main.ts && node main.js
main.ts(1,22): error TS2497: Module '"/Users/aleksandar/projects/typescript-playground/commonjs-function"' resolves to a non-module entity and cannot be imported using this construct.
这里也已经回答了问题,但它不适用于 typescript 2.0
How to write a typescript definition file for a node module that exports a function?
【问题讨论】:
标签: typescript commonjs typescript-typings typescript2.0