无法在React的 typescript 中导入文件

8xiog9wr  于 2023-02-13  发布在  TypeScript
关注(0)|答案(1)|浏览(173)

我得到很多错误,如:编译有问题:

ERROR in ./src/components/SearchSMS/Letters.tsx 122:71-81

export 'default' (imported as 'ChatWindow') was not found in './ChatWindow.js' (possible exports: __esModule)

但是我的PHPSorm IDE没有显示我的任何错误。我做了正确的导入。像:
x一个一个一个一个x一个一个二个x
我的package.json看起来像:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@types/react-router": "^5.1.20",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "^5.0.0",
    "web-vitals": "^3.0.0"
  },
  "scripts": {
    "start": "react-scripts --openssl-legacy-provider start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "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/node": "^18.13.0",
    "@types/react-dom": "^18.0.10",
    "ts-migrate": "^0.1.35",
    "typescript": "^4.9.5"
  }
}

我的tsconfig.json看起来像

{
{
  "compilerOptions": {

    "plugins": [{ "name": "typescript-plugin-css-modules" }],

    "target": "ES2018",                             
    "jsx": "preserve",                              

    "module": "CommonJS",                           

    "outDir": "./build",                            

    "esModuleInterop": true,                        
    "forceConsistentCasingInFileNames": true,       

    "strict": true,                                 

    "skipLibCheck": true,
     //and others commented settings                       
  },
  "exclude": ["node_modules"],
  "lib": ["esnext"],
  "ts-node": {
    "esm": true
  }
}

我试着在我的项目中从js转换到typescript,实际上,当我删除并尝试. tsc或npxtsc时,我甚至看不到编译后的文件

kmbjn2e3

kmbjn2e31#

我发现了我的问题的原因。这是因为“ts-migrate”包在同一个地方构建每一个ts-tsx文件。当我导入文件时,它不是从ts文件导入的,而是从坏的或旧的构建的js文件导入的。想象一下你有Home.ts和Home.js文件,当你

import Home from './Home';

从Home.js导入,但不从Home.ts导入

相关问题