简体   繁体   中英

'ITestData' is assignable to the constraint of type 'IEntityData', but 'IEntityData' could be instantiated with a different subtype of constraint

I have two classes. An abstract superclass:

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 }
}

and inherited class:

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 }
}

During build I have an error. I can not understand what I'm doing wrong.

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);

Oh my! Resolved! I just replaced this:

constructor(testData: ITestData) {
  super(testData);
}

with this:

constructor(testData: IEntityData) {
  super(testData);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM