如何从函数返回React.Component类?

时间:2018-02-09 16:31:31

标签: reactjs typescript

我已阅读此帖:How can I return a class from a TypeScript function?

在上面链接的帖子中,此工作代码显示为答案:

export class Foo {} // Foo is exported
export let factory = () : Foo => { // it could be return type of export function
    return Foo
};

我目前的代码是这样的:

export class SimpleElement extends React.Component<any, any> {}
export let test = (): SimpleElement => {
  return SimpleElement;
};

它给出了错误:

[ts] Type 'typeof SimpleElement' is not assignable to type 'SimpleElement'. Property 'setState' is missing in type 'typeof SimpleElement'.

我在这里查看了setState错误(Reactj, typescript Property 'setState' is missing in type),但答案似乎并没有特别有用。

1 个答案:

答案 0 :(得分:0)

想出来。使用typeof。

export class SimpleElement extends React.Component<any, any> {}
export let test = (): typeof SimpleElement => {
  return SimpleElement;
};