I am writing a web app using React with Typescript. I recently started some refactoring of old code to clean things up, and recieved this error:
TypeError: ___WEBPACK_IMPORTED_MODULE_0__.commonSortTitles is undefined
This is the code that causes the error:
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;
And the export commonSortTitles is here:
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;
In this setup, the IDE correctly reports: Image of what the intellisense shows.
Interestingly, if I move the commonSortTitles to the Bags.ts file, it begins working fine.
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;
This, however, means I have to duplicate the code for commonSortTitles in at least one other file, which I am working to avoid.
Here is my package.json file:
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"
}
}
Can anyone tell me what it is that might be causing this? It feels to me like this might be an issue with the React version, but I can't fathom why it hasn't come up until just now (since all of this code was in the project previously, I just moved some things around)
EDIT: Here is the index.ts for the folder these files are in '/components':
/components/index.ts
-------------------------
export * from './common';
export * from './Bags';
// additional exports that are unrelated
And here is the index file from the parent folder to that one:
index.ts
-----------------------------
export * from './components';
export * from './models';
export * from './redux';
Bypassing the index by directly importing from ./common.ts does indeed stop the error from occurring. Importing from '.' also works. Is there an issue with my import strategy?