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

问题描述 投票:0回答:1

我正在尝试使用 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)
reactjs typescript create-react-app
1个回答
1
投票

上周我花了太长时间才弄清楚这一点。在react-domexperimental.d.ts文件中:

要在实际项目中加载此处声明的类型,有以下三个 方式。最简单的一个,如果您的

tsconfig.json
已经有
"types"
"compilerOptions"
部分中的数组,是添加
"react-dom/experimental"
"types"
数组。

因此,将其添加到您的 tsconfig.json 中:

  "types" : [
    "react-dom/experimental"
  ]
© www.soinside.com 2019 - 2024. All rights reserved.