我正在尝试使用 TypeScript 创建 React 18 应用程序,我尝试遵循 此指南,但我无法使其工作。我无法将
"types": ["react/next", "react-dom/next"]
添加到 tsconfig 文件。我收到错误:
Cannot find type definition file for 'react-dom/next'.
The file is in the program because:
Entry point of type library 'react-dom/next' specified in compilerOptionsts
Cannot find type definition file for 'react-dom/next'.
The file is in the program because:
Entry point of type library 'react-dom/next' specified in compilerOptionsts
我也无法从
'react-dom/client';
导入 ReactDom 我收到此错误:
Could not find a declaration file for module 'react-dom/client'. 'C:/Users/bansc/Desktop/youtube-channel/api-calls/my-app/node_modules/react-dom/client.js' implicitly has an 'any' type.
If the 'react-dom' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-dom'ts(7016)
上周我花了太长时间才弄清楚这一点。在react-domexperimental.d.ts文件中:
要在实际项目中加载此处声明的类型,有以下三个 方式。最简单的一个,如果您的
已经有tsconfig.json
"types"
部分中的数组,是添加"compilerOptions"
到"react-dom/experimental"
数组。"types"
因此,将其添加到您的 tsconfig.json 中:
"types" : [
"react-dom/experimental"
]