我如何接受反应组件构造函数作为打字稿中的函数参数
how can i accept react component constructor as a function param in typescript
import { Component} from "react";
export class Test extends Component<{}, {}> {
render() {
return "Hello";
}
}
function myFunction<P, S, C extends Component<P, S>>(c: C): void {
console.log("ctor", c);
}
const s = myFunction(Test);
错误:
Argument of type 'typeof Test' is not assignable to parameter of type
'Component<{}, {}>'. Property 'setState' is missing in type 'typeof
Test'. 07:35:53 - Compilation complete. Watching for file changes.
我是否需要使用 super.(..) 实现原始反应 Component
class 的所有成员?
您需要将 C
的类型声明为扩展可返回组件的新函数的类型:
import { Component} from "react";
export class Test extends Component<{}, {}> {
render() {
return "Hello";
}
}
function myFunction<P, S, C extends new () => Component<P, S>>(c: C): void {
console.log("ctor", c);
}
const s = myFunction(Test);
import { Component} from "react";
export class Test extends Component<{}, {}> {
render() {
return "Hello";
}
}
function myFunction<P, S, C extends Component<P, S>>(c: C): void {
console.log("ctor", c);
}
const s = myFunction(Test);
错误:
Argument of type 'typeof Test' is not assignable to parameter of type 'Component<{}, {}>'. Property 'setState' is missing in type 'typeof Test'. 07:35:53 - Compilation complete. Watching for file changes.
我是否需要使用 super.(..) 实现原始反应 Component
class 的所有成员?
您需要将 C
的类型声明为扩展可返回组件的新函数的类型:
import { Component} from "react";
export class Test extends Component<{}, {}> {
render() {
return "Hello";
}
}
function myFunction<P, S, C extends new () => Component<P, S>>(c: C): void {
console.log("ctor", c);
}
const s = myFunction(Test);