我的用例非常简单,我想要一个动态反应组件并将其保存在注册表中:
import { Component } from 'react';
import DummyComponent from './DummyComponent';
class Registry {
TestComponent: Component = DummyComponent;
}
Run Code Online (Sandbox Code Playgroud)
DummyComponent.tsx:
import { Component } from 'react';
export default class DummyComponent extends Component {
constructor(props: any) {
super(props);
throw new Error('Cannot use this dummy component');
}
}
Run Code Online (Sandbox Code Playgroud)
这引发了 TypeScript 错误: ts(2740): Type 'typeof DummyComponent' is missing the following properties from type 'Component<{}, {}, any>': context, setState, forceUpdate, render, and 3 more.
我究竟做错了什么?我只想Registry.TestComponent成为一个 React 组件,它不必拥有所有这些属性,因为 React 组件不需要它们。
我做了一个StackBlitz 示例 …