我在基本服務中有以下方法:
crud-base.service.ts
export class CrudBaseService {
constructor(protected repo: MongoRepository<any>) {}
async create(data) {
// do stuff
}
}
在這里,我希望能夠創建我的自定義創建方法,因此我將我的方法命名為與基礎服務中的方法相同的方法
items.service.ts
@Injectable()
export class ItemsService extends CrudBaseService {
constructor(
@InjectRepository(Item)
private itemsRepository: MongoRepository<Item>,
) {
super(itemsRepository);
}
}
async create(data) {
// overriding base service method
}
給了我以下錯誤:
TS2416: Property 'create' in type 'ItemsService' is not assignable to the same property in base type 'CrudBaseService'
uj5u.com熱心網友回復:
兩個函式的簽名必須相同,即引數和回傳型別必須相同。你應該明確說明這一點。
export class CrudBaseService {
constructor(protected repo: MongoRepository<any>) {}
async create(data: YourType): Promise<YourReturnType> {
// do stuff
}
}
@Injectable()
export class ItemsService extends CrudBaseService {
constructor(
@InjectRepository(Item)
private itemsRepository: MongoRepository<Item>,
) {
super(itemsRepository);
}
}
// this should work
async create(data: YourType): Promise<YourReturnType> {
// overriding base service method
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/326514.html
上一篇:如何在基類中實體化泛型型別?
