如果我在index.ts中使用export *而不是export {Component1, Component2},模块创建是否有区别

问题描述 投票:0回答:1

我最近遇到了一次失败的测试,收到了以下错误消息”

TypeError: Cannot redefine property: getCurrentRouteSnapshot

我阅读了以下 StackOverflow,发现这可能是

__esModule
为 false 的结果,导致导入不可配置。 开玩笑间谍On不适用于索引文件,无法重新定义属性

虽然那里的解决方案有效,但感觉有点像创可贴修复,我很好奇为什么

__esModule
首先是错误的。

导出该辅助函数的索引文件包含以下内容:

export * from './router.tools';

我查看了我的

tsconfig.spec.json
,如下所示:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "module": "CommonJS",
    "target": "ES2016",
    "types": ["jest", "node", "chart.js"],
    "allowJs": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src/**/*.spec.ts", "../../types/**/*"]
}

我注意到

"module"
设置为
"CommonJS"

当我研究 CommonJS 导出标准时,我没有看到任何地方会使用

export *

如何将commonjs上的所有变量从不同的文件导出到一个地方

我更改了

index.ts
以改为导出以下内容(所有导出均单独):

export {getCurrentRouteSnapshot, GetCurrentApplication, routeToProbeFeature, ApplicationType, MonitorableTab} from './router.tools';

这修复了失败的测试。

为什么

export *
会导致不可配置,而
export {/*All Modules*/}
则不会?

angular typescript jestjs commonjs babel-jest
1个回答
0
投票

看起来像

module
属性,不应该是
commonjs
,因为
export * from './router.tools';
,所以请尝试根据应用程序的
module
更改
tsconfig.json
属性。

TSConfig 模块文档

您可以尝试选项

esnext
es6
node
,但最好是使用
tsconfig.json
中已有的选项。

示例 TSconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    ...
    "module": "esnext", 
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2016",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    ...
}

参考Stackblitz

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.