简体   繁体   English

我是否需要安装 @types/react 才能在 TypeScript 项目中使用 React?

[英]Do I need to install @types/react to work with React in a TypeScript project?

I'm converting my project from JavaScript to TypeScript.我正在将我的项目从 JavaScript 转换为 TypeScript。

This is my current package.json dependencies:这是我当前的package.json依赖项:

  "devDependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/core": "^7.10.5",
    "@babel/node": "^7.10.5",
    "@babel/preset-env": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "@babel/preset-typescript": "^7.10.4",
    "babel-loader": "^8.1.0",
    "babel-plugin-module-resolver": "^4.0.0",
    "babel-plugin-styled-components": "^1.10.7",
    "clean-webpack-plugin": "^3.0.0",
    "dotenv-webpack": "^2.0.0",
    "file-loader": "^6.0.0",
    "html-webpack-plugin": "^4.3.0",
    "ngrok": "^3.2.7",
    "rimraf": "^3.0.2",
    "webpack": "^4.44.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "@hot-loader/react-dom": "^16.13.0",
    "core-js": "^3.6.5",
    "firebase": "^7.17.1",
    "md5": "^2.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-helmet": "^6.1.0",
    "react-hot-loader": "^4.12.21",
    "react-redux": "^7.2.1",
    "react-router-dom": "^5.2.0",
    "redux": "^4.0.5",
    "regenerator-runtime": "^0.13.7",
    "styled-components": "^5.1.1",
    "uuid-v4": "^0.1.0"
  }
}

I've imported react and react-dom and I'm getting the following warnings:我已经导入了reactreact-dom ,但收到以下警告:

Could not find a declaration file for module 'react'.找不到模块“反应”的声明文件。 Try npm install @types/react if it exists or add a new declaration (.d.ts) file containing `declare module尝试npm install @types/react (如果它存在)或添加一个包含 `declare module 的新声明(.d.ts)文件

The same for react-dom react-dom也一样

Could not find a declaration file for module 'react-dom'.找不到模块“react-dom”的声明文件。 Try npm install @types/react-dom if it exists or add a new declaration (.d.ts) file containing `declare module尝试npm install @types/react-dom如果它存在或添加一个包含`declare module 的新声明(.d.ts)文件

Infact, all the libraries I've imported so far are giving me the same warning:事实上,到目前为止我导入的所有库都给我同样的警告:

index.tsx索引.tsx

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router} from 'react-router-dom';
import { Provider } from 'react-redux';

Do I need to install those @types/react to work with React with TypeScript?我是否需要安装那些@types/react才能与 TypeScript 一起使用 React? Will I also need to install the @types/package for every library that I import?我还需要为我导入的每个库安装@types/package吗? How does that work?这是如何运作的?

Yes.是的。 Or, well, to elaborate, to use a JS library with TypeScript, you'll need to或者,更详细地说,要使用带有 TypeScript 的 JS 库,您需要

  • use a library that ships TS types itself (many do, these days), or使用自己提供 TS 类型的库(如今很多人这样做),或者
  • install the related @types/ package (which stem from DefinitelyTyped );安装相关的@types/ package(源于DefinitelyTyped ); tooting my own horn here, I wrote a package called autotypes that can help , or在这里吹响我自己的号角,我写了一个package 可以帮助的自动类型,或者
  • write a .d.ts file for the library in your project's working cop, as below:在项目的 working cop 中为库编写一个.d.ts文件,如下所示:
declare module 'some-library' {
  export const doInterestingThings(): void;
}

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

相关问题 如何为 typescript 项目安装 React Beta 类型? - How to install react beta types for typescript project? 如何重构打字稿和反应项目中的一种类型表达式 - how do refactoring one types expression in typescript and react project Firebase 9 与 React Typescript:如何更改查询快照类型? - Firebase 9 with React Typescript: How do I change the querySnapshot types? 我是否需要安装react-tap-event-plugin才能使日期选择器在浏览器中正常工作? - do i need to install react-tap-event-plugin to make the date picker to work in the browser? 如果安装了@ types / react-router,是否需要安装react-router? - Need I install react-router if I installed @types/react-router? 我是否需要使用TypeScript + React将事件处理程序绑定到`this`? - Do I need to bind event handlers to `this` with TypeScript + React? 我需要在安装react-bootstrap之前先安装React吗 - Do I need to install React before I install react-bootstrap 如何在 typescript 反应项目中运行节点脚本? - How do I run a node script in a typescript react project? 在Typescript React项目中,如何实例化连接的组件? - In a Typescript React project how do I instantiate a connected component? 如何在React错误边界中使用自定义错误类型? - How do I work with custom error types in React error boundaries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM