javascript - 我找不到 `displayName` 的匹配 TypeScript 类型作为 `type` 的 Prop

标签 javascript reactjs typescript type-conversion

正如标题所述,我找不到正确的类型。
我已经检查了 DevTools 和我的每个组件,以编程方式查看 HAS Component.type.displayName .type对于不是 ElementType 的任何内容是一个函数。
节点的类型是 React.isValidElement 的返回类型,像这样提取 Parameters<typeof React.isValidElement> ,因为我不能确定它是什么。
有问题的部分:

const node = SomeThingUnknown as Parameters<typeof React.isValidElement>;
const type = ((node as unknown) as React.ReactElement<React.FunctionComponent>).type;
const displayName = typeof type === "function" ? type.displayName || type.name || "Unknown" : type;
最后一行取自 react repo 。所以我知道它有效。
node ,在 VS Code 中是:
const type: string | ((props: any) => React.ReactElement<any, string | ... | (new (props: any) =>
  React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)
当我将鼠标悬停在 displayName 上时的错误是:
Property 'displayName' does not exist on type 'JSXElementConstructor<any>'.
  Property 'displayName' does not exist on type '(props: any) =>
    ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null'.

最佳答案

尝试使用 Component.type.name而不是 Component.type.displayName

关于javascript - 我找不到 `displayName` 的匹配 TypeScript 类型作为 `type` 的 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65793723/

相关文章:

javascript - JQuery 使过多的动画队列

javascript - 有没有办法在 React 项目中使用 min.js 文件?这是一个好的做法吗?

node.js - 如何查看从node.js发送到服务器的请求?

javascript - 如何从另一个组件调用函数 [Angular]

javascript - PhoneRTC Swift 更新导致 ios 上没有音频

javascript - 在 JavaScript 中设置 window.location 时页面不重定向

javascript - snap svg 动画不起作用

javascript - NextJS Link 组件到底触发什么以及如何捕获该事件

javascript - 在按钮单击事件上渲染新的 react 组件

unit-testing - Firebase 函数 TypeScript 单元测试 devDependencies