这是代码
abstract class Model {
abstract fetch(): Partial<this>;
}
class X extends Model {
public a = 1;
fetch() {
return { a: 2 };
}
}
我试图在X.fetch中返回一个X类型,但我得到了这个
Property 'fetch' in type 'X' is not assignable to the same property in base type 'Model'.
Type '() => { a: number; }' is not assignable to type '() => Partial<this>'.
Type '{ a: number; }' is not assignable to type 'Partial<this>'.ts(2416)
您可以在一个单独的类中移动响应数据,并像这样使用它:
abstract class BaseModel {
abstract fetch(): any
}
class XDto{
public a: number = 1
}
class XModel extends XDto implements BaseModel {
fetch(): Partial<XDto> {
return {a: 1};
}
}
const x = new X()
const a = x.fetch()