reactjs - 如果 @types/react 和 react 的版本不同会有问题吗?
TypeScript 被添加到使用 create react app 创建的版本 16 的 React 应用中。我已经安装了最新的 v18 版本的 @types/react 和 @types/react-dom,但我想知道版本与 React 不同是否会有问题。谢谢。
答案#1
npx typesync 将解决类型同步问题。该脚本会检查你的 package.json 并在网上搜索你的包的 @types 版本(如果尚未安装的话)。如果你不再需要,也可能会删除这些 @types 包。
你可以更进一步,在你的 package.json 中添加这个:
“postinstall”: “npx typesync”
现在每次你安装或更新你的软件包时,你都会自动运行 postinstall 脚本!
答案#2
是的,因为你将拥有针对 React 18 的 TS 类型,但你的 JS 代码是针对 React 16 的。例如,你可能可以从 React 18 导入一个新特性,但由于你只有 React 16,可能会在运行时出现错误。
你可以通过使用 @types/react 和 @types/react-dom 版本 16 来解决这个问题,直到你准备好升级到 React 18。
