使用pptxgenjs遇TypeScript构造报错,求解决方法
解决PptxGenJS在TypeScript中无法实例化的问题
我是NodeJS新手,尝试在NodeJS应用的src/index.ts文件中编写如下代码:
import pptxgenjs from 'pptxgenjs'; const pres = new pptxgenjs();
(此写法来自PptxGenJS官方文档示例)
但TypeScript编译器报错:
This expression is not constructable.
Type 'typeof import("########/node_modules/pptxgenjs/types/index")' has no construct signatures.
我的tsconfig.json配置如下:
{ "compilerOptions": { "module": "NodeNext", "moduleResolution": "NodeNext", "target": "ES2020", "sourceMap": true, "outDir": "dist", "esModuleInterop": true, "allowSyntheticDefaultImports": true }, "include": ["src/**/*"] }
解决方案:
- 调整导入方式:PptxGenJS的TypeScript类型定义中,默认导出并非构造函数,需改用命名导入
PptxGenJS类:
import { PptxGenJS } from 'pptxgenjs'; const pres = new PptxGenJS();
- 更新依赖版本:旧版本的PptxGenJS可能存在类型定义缺陷,执行命令更新到最新版:
npm install pptxgenjs@latest
- 适配模块解析规则:若上述方法无效,可修改
tsconfig.json的模块配置,将module和moduleResolution改为Node16或CommonJS:
{ "compilerOptions": { "module": "CommonJS", "moduleResolution": "Node", "target": "ES2020", "sourceMap": true, "outDir": "dist", "esModuleInterop": true, "allowSyntheticDefaultImports": true }, "include": ["src/**/*"] }
内容的提问来源于stack exchange,提问作者user20416
相关产品推荐
相关产品推荐

