我讓它與常規類一起使用,包括擴展目標類的類,如下所示:
interface ALike {
a: () => boolean
}
interface ALikeConstructor {
new (): ALike
aStatic: () => boolean
}
function acceptsALike(ctr: ALikeConstructor) { }
class A {
public a() {
return true;
}
public static aStatic(): boolean {
return true;
}
}
class AB extends A {
public b() {
return true;
}
}
class ANoStatic {
public a() {
return true;
}
}
acceptsALike(A); // ok
acceptsALike(AB); // ok
acceptsALike(ANoStatic); // ok (error shows)
操場
但是,無論出于何種原因,它都不適用于 React 組件,TypeScript 也沒有給出太多解釋。
import React from 'react'
type Props = {
test: true
}
interface CLike extends React.Component<Props> {
c(): boolean
}
interface CLikeConstructor {
new(): CLike
cStatic(): boolean
}
class C extends React.Component<Props> implements CLike {
public c() {
return true;
}
public static cStatic() {
return true;
}
}
function acceptsCLike(ctr: CLikeConstructor) { }
acceptsCLike(C); // Argument of type 'typeof C' is not assingable to parameter of type 'CLikeConstructor' - no further explanation.
操場
我知道 React.Component 的某些東西使它不兼容,但它是什么,有沒有辦法解決它?
uj5u.com熱心網友回復:
問題是您的建構式簽名 (in CLikeConstructor) 描述了一個沒有引數的建構式。react 類有一個帶有一個引數(props)的建構式。
您可以將建構式簽名定義為不關心 args 的數量:
interface CLikeConstructor {
new(...a: any[]): CLike
cStatic(): boolean
}
游樂場鏈接
您也可以讓它只接受一個引數(道具),如下所示:new(props: any): CLike
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/510416.html
