![](/img/trans.png)
[英]Type is assignable to the constraint of type 'Type', but 'Type' could be instantiated with a different subtype of constraint 'Type'
[英]'ITestData' is assignable to the constraint of type 'IEntityData', but 'IEntityData' could be instantiated with a different subtype of constraint
我有两节课。 抽象超类:
export abstract class Entity<IEntityData> {
protected readonly _source: IEntityData; // TODO: replace with private
protected constructor(entityData: IEntityData) { this._source = entityData }
get source(): IEntityData { return this._source }
}
和继承的类:
import { Entity } from './entity/entity';
interface ITestData {
foo: string;
bar: number;
}
export class Test<IEntityData extends ITestData = ITestData>
extends Entity<IEntityData> {
constructor(testData: ITestData) {
super(testData);
}
get foo(): string { return this.source.foo }
get bar(): number { return this.source.bar }
}
在构建期间我有一个错误。 我不明白我做错了什么。
src/app/data/models/test.ts:13:11 - error TS2345: Argument of type 'ITestData' is not assignable to parameter of type 'IEntityData'.
'ITestData' is assignable to the constraint of type 'IEntityData', but 'IEntityData' could be instantiated with a different subtype of constraint 'ITestData'.
13 super(testData);
天啊! 解决! 我只是替换了这个:
constructor(testData: ITestData) {
super(testData);
}
有了这个:
constructor(testData: IEntityData) {
super(testData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.