javascript - React Typescript-fel "TypeError: ___WEBPACK_IMPORTED_MODULE_0__.commonSortTitles är odefinierad"


Jag skriver en webbapp med React och Typescript. Jag började nyligen med lite refaktorisering av gammal kod för att städa upp saker, och fick detta fel:

TypeError: ___WEBPACK_IMPORTED_MODULE_0__.commonSortTitles is undefined

Detta är koden som orsakar felet:

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;

Och exporten commonSortTitles är här:

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;

I denna uppställning rapporterar IDE korrekt: Bild av vad intellisense visar.

Intressant nog, om jag flyttar commonSortTitles till filen Bags.ts, börjar det fungera bra.

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;

Detta innebär dock att jag måste duplicera koden för commonSortTitles i minst en annan fil, vilket jag försöker undvika.

Här är min package.json-fil:

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"
  }
}

Kan någon berätta för mig vad det är som kan orsaka detta? Det känns för mig som om detta kan vara ett problem med React-versionen, men jag kan inte förstå varför det inte har dykt upp förrän just nu (eftersom all denna kod redan fanns i projektet tidigare, jag flyttade bara runt några saker)

REDIGERA: Här är index.ts för mappen som dessa filer finns i '/components':

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

Och här är indexfilen från mappens förälder till den:

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

Att kringgå indexet genom att direkt importera från ./common.ts stoppar faktiskt att felet uppstår. Att importera från '.' fungerar också. Finns det ett problem med min importstrategi?

Relaterade artiklar