简体   繁体   English

无法将 typescript 添加到反应项目

[英]Unable to add typescript to a react project

I'm trying to add typescript to a existing react project but I'm getting an error when re-starting the server with npm start.我正在尝试将 typescript 添加到现有的反应项目中,但是在使用 npm 启动重新启动服务器时出现错误。

I'm following the steps from this documentation but I'm getting an error message:我正在按照文档中的步骤操作,但收到一条错误消息:

在此处输入图像描述

The only thing I might not have right is the react.scripts versions.我唯一可能不正确的是 react.scripts 版本。 Here is a copy of the results thrown by npm outdated:这是 npm outdated 抛出的结果的副本:

在此处输入图像描述

Also, I checked my dependencies and everything seems to be normal.另外,我检查了我的依赖项,一切似乎都很正常。

Things I have done:我做过的事情:

  • run npm update to update all react related packages运行 npm update 更新所有 react 相关的包
  • I have deleted the node files and re install them with npm install我已删除节点文件并使用 npm install 重新安装它们
  • I have renamed other files to.tsx including the index.js file but I get another error:我已将其他文件重命名为 .tsx,包括 index.js 文件,但出现另一个错误:

在此处输入图像描述

Really keen to move this project to the next level with typescript but I have been stack trying to get it to work.真的很想用 typescript 将这个项目提升到一个新的水平,但我一直在努力让它工作。 By the way, here are my dependencies too:顺便说一句,这也是我的依赖项:

{
  "name": "world-budget-react-v",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.34",
    "@fortawesome/free-brands-svg-icons": "^5.15.2",
    "@fortawesome/free-solid-svg-icons": "^5.15.2",
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@types/jest": "^26.0.23",
    "@types/node": "^15.0.2",
    "@types/react": "^17.0.5",
    "@types/react-dom": "^17.0.3",
    "@types/react-redux": "^7.1.16",
    "autoprefixer": "7.1.6",
    "axios": "^0.21.1",
    "babel-core": "6.26.0",
    "babel-eslint": "7.2.3",
    "babel-jest": "20.0.3",
    "babel-loader": "7.1.2",
    "babel-preset-react-app": "^3.1.2",
    "babel-runtime": "6.26.0",
    "case-sensitive-paths-webpack-plugin": "2.1.1",
    "chalk": "1.1.3",
    "css-loader": "0.28.7",
    "dotenv": "4.0.0",
    "dotenv-expand": "4.2.0",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.6",
    "eslint": "4.10.0",
    "eslint-config-react-app": "^2.1.0",
    "eslint-loader": "1.9.0",
    "eslint-plugin-flowtype": "2.39.1",
    "eslint-plugin-import": "2.8.0",
    "eslint-plugin-jsx-a11y": "5.1.1",
    "eslint-plugin-react": "7.4.0",
    "extract-text-webpack-plugin": "3.0.2",
    "file-loader": "1.1.5",
    "firebase": "^8.2.6",
    "firebase-admin": "^9.4.2",
    "firebase-functions": "^3.13.1",
    "fs-extra": "3.0.1",
    "html-webpack-plugin": "2.29.0",
    "jest": "20.0.4",
    "object-assign": "4.1.1",
    "postcss-flexbugs-fixes": "3.2.0",
    "postcss-loader": "2.0.8",
    "promise": "8.0.1",
    "raf": "3.4.0",
    "react": "^17.0.2",
    "react-dev-utils": "^5.0.2",
    "react-dom": "^17.0.2",
    "react-gtm-module": "^2.0.11",
    "react-redux": "^7.2.2",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "react-scroll-restoration": "^1.0.6",
    "react-test-renderer": "^17.0.1",
    "redux": "^4.1.0",
    "redux-thunk": "^2.3.0",
    "resolve": "1.6.0",
    "style-loader": "0.19.0",
    "sw-precache-webpack-plugin": "0.11.4",
    "typescript": "^4.2.4",
    "url-loader": "0.6.2",
    "webpack": "3.8.1",
    "webpack-dev-server": "2.11.3",
    "webpack-manifest-plugin": "1.3.2",
    "whatwg-fetch": "2.0.3"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js --env=jsdom"
  },
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
    ],
    "testEnvironment": "node",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  },
  "eslintConfig": {
    "extends": "react-app"
  }
}

Any recommendations?有什么建议吗? I hope I provided as much info as possible!我希望我提供尽可能多的信息!

Lastly, I found this answer but not sure what they mean.最后,我找到了这个答案,但不确定它们的意思。 Anyone keen to explain maybe Could not find a required file.任何热衷于解释的人都可能找不到所需的文件。 - Adding TypeScript to React project - 将 TypeScript 添加到 React 项目

As per the react-scripts 2.10 you can add TypeScript to an existing project.根据 react-scripts 2.10,您可以将 TypeScript 添加到现有项目中。

yarn add typescript @types/node @types/react @types/react-dom @types/jest --dev

Create a react-native app using: npx react-native init AwesomeProject使用以下命令创建 react-native 应用程序: npx react-native init AwesomeProject

Once its up and running, follow this link to add typescript the right way.一旦启动并运行,请按照此链接以正确的方式添加 typescript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM