使用打字稿创建 React 应用程序 React 18



我正在尝试使用 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-dom experimental.d.ts 文件中:

若要在实际项目中加载此处声明的类型,有三个 方式。最简单的一个,如果你的tsconfig.json已经有"types"数组在"compilerOptions"部分中,是要添加的"react-dom/experimental""types"数组。

所以,把它添加到你的tsconfig.json:

"types" : [
"react-dom/experimental"
]

最新更新