@types/tabulator-tables 中断制表符表导入

@types/tabulator-tables breaking tabulator-tables import

正在安装以下内容(从 tabulator-tables@4.9.1 开始):

npm install tabulator-tables
npm install @types/tabulator-tables

然后执行以下导入

import Tabulator from 'tabulator-tables';

导致错误:Module Usage (Error node_modules @types tabulator tables index.d.ts' is not a module)

改为使用此导入

import 'tabulator-tables';

不输出任何错误,并允许加载类型信息,但在运行时(例如,在 Angular 12 项目上)实际上不允许访问 Tabulator 对象,导致像 ReferenceError: Tabulator is not defined.

这是由于 @types/tabulator-tables 中的类型定义文件中缺少默认导出。不幸的是,在上游添加默认导出是不可行的,因为它会破坏现有的代码库。

幸运的是有一个解决方法,described here:如果您还没有创建一个 index.d.ts 文件,并向其中添加以下行:

declare module "tabulator-tables" { export = Tabulator; }

(一定要通过在 "files" 中列出 index.d.ts 文件或确保它与 [=25= 中的 "includes" 中的模式相匹配来确保包含 index.d.ts 文件]。您可能还需要在 "compilerOptions" 中设置 "allowSyntheticDefaultImports": true。)