javascript - 反应 Typescript 错误“TypeError:___WEBPACK_IMPORTED_MODULE_0__.commonSortTitles 未定义”
问题描述
我正在使用 React 和 Typescript 编写一个 Web 应用程序。我最近开始对旧代码进行一些重构以清理内容,并收到此错误:
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 正确报告:智能感知显示的图像。
有趣的是,如果我将 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 导入来绕过索引确实可以阻止错误的发生。从“。”导入 也有效。我的导入策略有问题吗?
解决方案
推荐阅读
- amazon-cloudformation - 使用 Lambda 函数自动创建的角色的 ARN
- python - 获取列表的所有子列表的快速方法
- java - Java 正在连接到 SQL 数据库,但它没有更新表
- angular - Angular 5 - 从 API 调用返回 HttpResponse 时在模板中绑定 Observable 数据
- c++ - 是否可以跳过/删除 ELF 文件中的“PHDR”程序头以执行可执行文件?如果是这样,为什么?
- spring - 如何在没有 Spring Boot 的情况下将带有 Spring5 WebClient 的项目打包成 JAR 文件?
- r - R中的条形图,x轴为年份
- .htaccess - 如何隐藏 url 子文件夹?
- python - Python 请求后 Instagram 导航不起作用
- c# - C# 长度为 1 的数组与单值、性能和内存开销