typescript - 设置 typescript 以使用 require 和 import 语法
问题描述
我在设置我的打字稿项目以与 ES 模块正常工作时遇到问题。我经历了几个现有的线程,但我还没有看到一个合适的解决方案。我感觉自己陷入了无限循环。
我正在尝试使用import
语法正确导入 pdfmake,但我不断收到未知文件扩展名 .ts 的错误,或者在type: "module"
从package.json
错误中删除后,因为它无法加载 ES 模块而不type: "module"
位于.ts 文件中package.json
。
我不确定解决方法是什么,但我需要将"module"
tpye 保留在我的package.json
. 我尝试添加"esModuleInterop": true
,tsconfig.json
但也没有用。
有没有办法建立一个打字稿项目来支持require
和import
语法?
这是index.ts
import * as pdfMake from 'pdfmake/build/pdfmake.js';
import * as pdfFonts from 'pdfmake/build/vfs_fonts.js';
import * as fs from 'fs'
import * as jsdom from 'jsdom'
import htmlToPdfMake from 'html-to-pdfmake'
pdfMake.vfs = pdfFonts.pdfMake.vfs;
let { JSDOM } = jsdom;
let { window } = new JSDOM("");
这是我的tsconfig
{
"compileOnSave": true,
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true,
"target": "ES5",
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"typeRoots": [],
"types": [] //Explicitly specify an empty array so that the TS2 @types modules are not acquired since we aren't ready for them yet.
},
"exclude": ["node_modules"]
}
解决方案
推荐阅读
- jquery - 在 jquery 中禁用浏览器弹出窗口阻止程序
- python - 将numpy数组中的单个值提取到列表中
- sql - 链接第三张桌子
- sql - 如何使用 SQL 交叉连接而不是 UNPIVOT?
- html - 下拉切换的样式按钮取决于 nav-link 的 RouterLinkActive
- java - RxJava - 如何使用内部“toBlocking”调用测试函数
- python - 从 keras.layers 导入 LayerNormalization 时出错
- kubernetes - 如何为特定目标动态更改普罗米修斯运算符中的scrape_interval
- r - 如何在R中按组对NA行求和
- python - 错误:创建版本失败。检测到错误的模型错误:“加载模型失败:无法加载模型