首页 > 解决方案 > TypeScript 可传递地导入 ES 库

问题描述

我想确保我的代码不使用比 ES6 更新的库功能。因此,我的tsconfig.json样子是这样的:

{
  "compilerOptions": {
    "lib": [
      "es6"
    ],
    "target": "es6",
    "outDir": "./dist",
    "rootDir": "./src",
    "module": "commonjs",
    "declaration": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  }
}

这按预期工作,并且类似的调用Object.fromEntries被标记为错误。

可悲的是,一旦我安装了一些依赖于@types/node的开发依赖项,例如 Rollup 插件,TypeScript 就会选择以下声明node_modules/@types/node

/// <reference lib="es2018" />

(文件node_modules/@types/node/ts3.7/base.d.ts)。

突然,Object.fromEntries不再被标记,因为——尽管我在其中进行了配置tsconfig.json——TypeScript 现在加载了 ES2018 库。

有什么办法可以避免吗?

标签: typescript

解决方案


推荐阅读