例如,我有这个Hook
组件:
const Color = ():JSX.Element => <div>Red</div>
但是出于好奇,我如何将其转换为基于 Class
的组件?
我知道我可以像这样拥有 Props
和 State
class Color extends React.Component<Props, State>{
render(){ <div>Red</div>}
}
是否可以检查基于Class
的组件中的返回值JSX.Element
?
最佳答案
您可以访问 render
的类型方法 Color
像这样的类组件:Color["render"]
。您可以使用内置实用程序类型 ReturnType<T extends (...args: any) => any>
捕获返回类型。 。 Here's the full code in a TypeScript playground .
关于javascript - 如何使用 Typescript 检查 React 中基于类的组件的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699856/