【问题标题】:Could not find a declaration file for module 'react-dom'找不到模块“react-dom”的声明文件
【发布时间】:2021-01-06 00:27:10
【问题描述】:

我已经用 Typescript 安装了 React 项目:

npx create-react-app my-app --template typescript

在我使用npm run start 运行应用程序之后

我收到此错误:

TypeScript error in C:/Users/neman/Desktop/projects/redux-saga-ts-boilerplate/src/index.tsx(2,22):
Could not find a declaration file for module 'react-dom'. 'C:/Users/neman/Desktop/projects/redux-saga-ts-boilerplate/node_modules/react-dom/index.js' implicitly has an 'any' type.
  If the 'react-dom' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-dom`  TS7016

    1 | import React from 'react';
  > 2 | import ReactDOM from 'react-dom';
      |                      ^
    3 | import './index.css';
    4 | import App from './App';
    5 | import * as serviceWorker from './serviceWorker';

这是我的package.json

{
  "name": "redux-saga-ts-boilerplate",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "@types/redux-actions": "^2.6.1",
    "@types/redux-logger": "^3.0.7",
    "axios": "^0.19.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.0",
    "react-scripts": "3.4.1",
    "redux": "^4.0.5",
    "redux-actions": "^2.6.5",
    "redux-logger": "^3.0.6",
    "redux-saga": "^1.1.3",
    "typescript": "~3.7.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "reinstall": "rm -rf ./node_modules && npm install",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "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"
    ]
  }
}

如你所见,我确实安装了"@types/react-dom"

这是我的tsconfig.json 文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["src"]
}

那么,这里有什么问题呢?

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    我有同样的问题。在遇到 answer 后发现了一个稍微不同的问题。试试这个:

    npm install --save @types/react-dom

    希望这对你有用!

    【讨论】:

      【解决方案2】:

      如果您的项目工作仍未解决,这是一个非常常见的问题,但这种工作方式不是很好,研究每件事并解决该问题,然后必须去。

      npm install --save @types/react-dom
      

      这个命令有效。

      【讨论】:

        猜你喜欢
        • 2019-03-16
        • 2019-07-26
        • 2021-02-21
        • 2017-05-29
        • 2022-06-12
        • 2022-11-06
        • 2021-01-18
        • 2021-09-03
        • 2021-08-25
        相关资源
        最近更新 更多