reactjs - 合并反应打字稿命名空间声明
问题描述
我正在使用带有 Typescript 的 React。目前@types/react 提供的 ReactElement 定义是错误的——它们不允许 ReactElement 是例如字符串或数字。据我了解,由于向后兼容性,这不会被修复。
因此,我认为我可以在本地修复这些声明,但我无法使其工作。这是我尝试过的:
我有一个global.d.ts
文件,我在其中保留了一些额外的声明(如扩展窗口等)。我添加了这段代码:
declare namespace React {
type ReactElement<
P = any,
T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>
> =
| {
type: T
props: P
key: Key | null
}
| React.ReactNode
}
所以基本上我希望 ReactElement 也包含 React.ReactNode 声明。
原始 ReactElement 声明如下:
declare namespace React {
...
interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
type: T;
props: P;
key: Key | null;
}
...
}
解决方案
推荐阅读
- python - 从 javascript 到 Ruby 或其他语言的三重 DES 加密
- python-3.x - 在 Mac OS 的 Visual Studio Code 中将 Homebrew python3 与 homebrew 包和 pip/pip3 包一起使用
- laravel - 在 laravel 错误中为 foreach() 提供的参数无效(查看:我看不到 $value
- reactjs - React 中的引导模式未打开
- colorbar - .jpeg 热图像中不准确的颜色图 [FLIR TOOLS]
- docker - 开发环境的 docker-compose 和 docker-compose.extend 之间的映射端口
- kotlin - Kotlin 智能转换无法使用扩展功能
- swift - 带有授权不记名令牌和附加标头的 Alamofire 请求 Swift
- gradle - 此版本中使用了弃用的 Gradle 功能,使其与 Gradle 7.0 不兼容
- azure - nginx入口控制器不读取配置映射