正在做什么
yarn add react-router-dom
失败。应用程序在启动时会抛出一个错误,Intellij会将react-router-dom的导入标记为错误。但是在做
yarn add @types/react-router-dom
很管用。
不过,我找到的所有示例都显示了第一个yarn add...命令。这是react-router-dom的新功能吗?
我正在使用typescript,如果这有什么不同的话。
===Edit===
以下是错误消息。这就是我找到解决方案的地方。
/home/dean/src/react/projects/room-reservations-ui_/src/App.tsx
TypeScript error in /home/dean/src/react/projects/room-reservations-ui_/src/App.tsx(5,29):
Could not find a declaration file for module 'react-router-dom'. '/home/dean/src/react/projects/room-reservations-ui_/node_modules/react-router-dom/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/react-router-dom` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-router-dom';` TS7016
3 | import './App.css';
4 | import Navbar from "./components/Navbar";
> 5 | import {BrowserRouter} from "react-router-dom";
| ^
6 |
7 | function App() {
8 | return (发布于 2021-09-01 01:08:33
yarn add react-router-dom这是在不使用任何类型的情况下安装react-router-dom,如果您尝试仅使用此软件包启动应用程序,您将看到收到的消息
Could not find a declaration file for module 'react-router-dom'.这并不意味着react-router-dom有错误,但Typescript没有为其声明类型,项目可能是用Javascript创建的,或者类型声明在其他地方。
yarn add @types/react-router-dom这将安装来自DefinitelyTyped(https://github.com/DefinitelyTyped/DefinitelyTyped)的react-router-dom的类型,这是一个存储库,其中进行了声明,因此我们可以为仅为Javascript创建的库安装类型,但其他人却为我们“输入”了它。
@types/这个前缀是一个组织,它将引用DefinitelyTyped存储库中的react-router-dom包,它不包含react-router-dom代码,只包含类型定义,您可以在这里看到https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-router-dom
它只包含声明,当Typescript找不到您正在使用的模块的声明时,它将无法正确地显示类型,因此它会给出警告
发布于 2021-09-01 01:07:24
简单地说,@types/react-router-dom包含类型定义。它旨在与typescript一起使用。您可以查看有关此here的更多信息。
发布于 2021-11-07 08:39:10
我也有同样的问题。刚刚从带有类型的节点模块中删除了rrd,并重新安装了它
https://stackoverflow.com/questions/69006604
复制相似问题