`yarn add react-router-dom` 和 `yarn add @types/react-router-dom` 有什么区别?

迪恩·舒尔茨

正在做

yarn add react-router-dom

失败。该应用程序在启动时抛出错误,Intellij 会将从中导入的内容标记react-router-dom为错误。但是做

yarn add @types/react-router-dom

作品。

不过,我发现的所有示例除了一个都显示了第一个yarn add...命令。这是新东西react-router-dom吗?

如果这有什么不同,我正在使用打字稿。

===编辑===

这是错误消息。这是我找到解决方案的地方。

/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 (
连在一起
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

这将安装来自绝对类型(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 找不到您正在使用的模块的声明时,它将无法正确显示类型,因此它会发出警告

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章