reactjs - What's the difference between `yarn add react-router-dom` and `yarn add @types/react-router-dom`?
问题描述
Doing
yarn add react-router-dom
fails. The app throws an error on startup and Intellij flags the import from react-router-dom
it as an error. But doing
yarn add @types/react-router-dom
works.
All the examples I've found except for one show the first yarn add...
command, though. Is this something new with react-router-dom
?
I'm using typescript if that makes any difference.
===Edit===
Here is the error message. This is where I found the solution.
/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 (
解决方案
Simply put, @types/react-router-dom
contains type definitions. It is intended for use with typescript. you can see more about this here.
推荐阅读
- javascript - 实现 js 类属性的钩子
- rust - 移动到过滤器闭包中的特征参数的生命周期限制应该是什么?
- javascript - ReactJS 返回待处理的承诺
- angular - 嵌套表单数组问题:错误错误:找不到带有路径的控件:'问题 - > 0 - > 代码'
- c - 双向通信管道
- regex - 正则表达式未捕获所需数据
- c++ - C++ 函数不更新变量
- python - 来自 main 的局部变量被其他程序的函数修改,即使它不应该
- postgresql - 由于 Docker 上的权限被拒绝,无法创建 postgres 用户
- c++ - CMake“find_package”找到了包但编译器找不到包含文件