簡體   English   中英

`yarn add react-router-dom` 和 `yarn add @types/react-router-dom` 有什么區別?

[英]What's the difference between `yarn add react-router-dom` and `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 (

簡單地說, @types/react-router-dom包含類型定義。 它旨在與打字稿一起使用。 你可以在這里看到更多關於這個的信息

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 找不到您正在使用的模塊的聲明時,它將無法正確顯示類型,因此它會發出警告

我有同樣的問題。 剛剛從具有類型的節點模塊中刪除了 rrd 並重新安裝了它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM