简体   繁体   English

types/react 和 types/react-dom 库在 React 中的作用是什么?

[英]What is the purpose of types/react and types/react-dom libraries in react?

I am working with react 18 in typescript. However, apart from react and react-dom, I also see types/react and types/react-dom libraries in package.json.我正在 typescript 中使用 React 18。但是,除了 React 和 React-dom 之外,我还在 package.json 中看到了 types/react 和 types/react-dom 库。 What is the purpose of these libraries?这些图书馆的目的是什么?

The ones with @types prefix are type declaration packages.带有@types 前缀的是类型声明包。

  • Some libraries are now including the types in the library for you.一些图书馆现在为您包括图书馆中的类型。 Couldn't find the types file for Axios, simply because they have already provided the types, no need for an extra types file.找不到 Axios 的类型文件,只是因为他们已经提供了类型,不需要额外的类型文件。
  • @types/react and @types/react-dom are optional, and are not needed to use React or TypeScript. If you are not using TypeScript in your project, or if you are satisfied with the type inference provided by the TypeScript compiler, you may not need to install these packages. @types/react 和@types/react-dom 是可选的,并且不需要使用 React 或 TypeScript。如果你的项目中没有使用 TypeScript,或者你对 TypeScript 编译器提供的类型推断感到满意,你可能不需要安装这些包。

It is most commonly found in React Typescript. As in Typescript, we have to define types of variables.它最常见于 React Typescript。与 Typescript 一样,我们必须定义变量的类型。 So when we use React with Typescript, we use types/react to declare the type of the React library and types/react-dom to declare the type of the React-dom library.所以当我们使用React with Typescript时,我们使用types/react来声明React库的类型,使用types/react-dom来声明React-dom库的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM