首页 > 解决方案 > React Typescript - import @types/react 和 import react 有什么区别

问题描述

我是 React 的初学者(即使我已经做了一些 ReactJs),我今天正在尝试使用TypeScript (instead of pure Javascript).

但是,在运行 npm start 时,我发现了一个我理解的问题,但我不明白为什么它是一个问题。

假设我们有这段代码(main.tsx):

import * as React from 'react';

你能清楚地向我解释一下有什么区别吗

import * as React from '@types/react';

在我的 package.json 中"@types/react": "^16.9.2",我有也没有react,所以问题是找不到包的逻辑。但是,这两个进口之间有什么区别?

另外,我什至不能导入@types/react,我只能安装它

谢谢 !

标签: javascriptreactjstypescriptimport

解决方案


带有@types前缀的是类型声明包,更多信息在这里


推荐阅读