[英]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:我已经导入了
react
和react-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 库,您需要
@types/
package (which stem from DefinitelyTyped );@types/
package(源于DefinitelyTyped ); tooting my own horn here, I wrote a package called autotypes that can help , or.d.ts
file for the library in your project's working cop, as below:.d.ts
文件,如下所示:declare module 'some-library' {
export const doInterestingThings(): void;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.