反应打字稿错误“TypeError:___WEBPACK_IMPORTED_MODULE_0__.commonSortTitles 未定义”

时间:2021-04-10 04:38:08

标签: javascript reactjs typescript webpack

我正在使用 React 和 Typescript 编写一个网络应用程序。我最近开始对旧代码进行一些重构以清理内容,并收到此错误:

TypeError: ___WEBPACK_IMPORTED_MODULE_0__.commonSortTitles is undefined

这是导致错误的代码:

Bags.ts
-------------------------------------------
import { Bag, commonSortTitles } from '..';

// code here that does not affect the issue

export const bagSortTitles = [
  ...commonSortTitles,
  '# of Magic Items',
  '# of Items'
] as const;

导出 commonSortTitles 在这里:

common.ts
-------------------------------------------------------------------
export type SortDirection = typeof ascendingTitles[number];

export const commonSortTitles = [
  'Created',
  'Last Updated',
  'Weight',
  'Title'
] as const;

export const ascendingTitles = ['Ascending', 'Descending'] as const;

在此设置中,IDE 正确报告:Image of what the intellisense shows.

有趣的是,如果我将 commonSortTitles 移动到 Bags.ts 文件,它开始工作正常。

Bags.ts (workaround)
-------------------------------------------
import { Bag } from '..';

// code here that does not affect the issue

const commonSortTitles = [
  'Created',
  'Last Updated',
  'Weight',
  'Title'
] as const;

export const bagSortTitles = [
  ...commonSortTitles,
  '# of Magic Items',
  '# of Items'
] as const;

然而,这意味着我必须至少在另一个文件中复制 commonSortTitles 的代码,我正在努力避免这种情况。

这是我的 package.json 文件:

package.json
---------------------------------------------------------
{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.3",
    "@material-ui/icons": "^4.11.2",
    "@material-ui/lab": "^4.0.0-alpha.57",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "@types/jest": "^24.9.1",
    "@types/node": "^12.20.7",
    "@types/react": "^16.14.5",
    "@types/react-dom": "^16.9.12",
    "@types/react-google-recaptcha": "^1.1.1",
    "@types/react-helmet": "^5.0.15",
    "@types/react-redux": "^7.1.16",
    "@types/react-router": "^5.1.13",
    "@types/react-router-dom": "^5.1.7",
    "axios": "^0.21.1",
    "cross-env": "^7.0.3",
    "enzyme": "^3.11.0",
    "jquery": "^3.6.0",
    "node-sass": "^4.14.1",
    "react": "^16.14.0",
    "react-dom": "^16.14.0",
    "react-google-recaptcha": "^2.0.1",
    "react-helmet": "^6.0.0",
    "react-redux": "^7.2.3",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^4.0.3",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "typescript": "^4.2.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "tslint src/**/*.ts",
    "lint:fix": "npm run lint -- --fix"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/redux-logger": "^3.0.7",
    "@typescript-eslint/eslint-plugin": "^4.20.0",
    "@typescript-eslint/eslint-plugin-tslint": "^4.20.0",
    "@typescript-eslint/parser": "^4.20.0",
    "eslint": "^7.23.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.1.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsdoc": "^32.3.0",
    "eslint-plugin-no-null": "^1.0.2",
    "eslint-plugin-react": "^7.23.1",
    "eslint-plugin-react-hooks": "^4.2.0",
    "react-lorem-ipsum": "^1.4.9"
  }
}

谁能告诉我是什么导致了这种情况?我觉得这可能是 React 版本的问题,但我无法理解为什么它直到现在才出现(因为所有这些代码之前都在项目中,我只是移动了一些东西)

编辑: 这是这些文件位于“/components”中的文件夹的 index.ts:

/components/index.ts
-------------------------
export * from './common';
export * from './Bags';
// additional exports that are unrelated

这是从父文件夹到那个文件夹的索引文件:

index.ts
-----------------------------
export * from './components';
export * from './models';
export * from './redux';

通过直接从 ./common.ts 导入绕过索引确实阻止了错误的发生。从“.”导入也有效。我的导入策略有问题吗?

0 个答案:

没有答案