【发布时间】:2019-09-14 18:05:32
【问题描述】:
我已经安装了npm包https://github.com/alferov/array-to-tree/
npm install array-to-tree --save
在它的 index.d.ts 文件中有函数和命名空间的声明:
export = arrayToTree;
declare function arrayToTree<T>(data: T[], options?: Partial<arrayToTree.Options>): Array<arrayToTree.Tree<T>>;
declare namespace arrayToTree {
interface Options {
childrenProperty: string;
parentProperty: string;
customID: string;
rootID: string;
}
type Tree<T> = T & {
children?: Array<Tree<T>>;
};
}
我尝试以这种方式在我的 Angular 应用程序中导入它:
import * as arrayToTree from 'array-to-tree';
但是我得到了编译错误:
错误 TS2307:找不到模块“arrayToTree”。
我也尝试使用 CommonJS 样式导入:
import arrayToTree = require('array-to-tree');
代码编辑器出错:
TS1202:以 ECMAScript 模块为目标时无法使用导入分配。考虑改用'import * as ns from "mod"'、'import {a} from "mod"' 或'import d from "mod"'。
我尝试了所有建议的变体,但没有解决问题。
我认为这个问题可能与 npm-package 中的 package.json 文件有关,因为它没有选项 "typings": "index.d.ts"。但是我尝试添加此选项并没有得到任何结果。
我认为问题在于将命名空间与函数一起导入。 或者可能是兼容性 Typescript 版本和 index.d.ts 文件格式的问题?我使用 Typescript 3.2.2。
我做错了什么?
更新: 我在stackblitz上做例子: https://stackblitz.com/edit/angular-ehjdfy
控制台有错误。
【问题讨论】:
标签: angular typescript node-modules typescript-typings commonjs