简体   繁体   English

我可以为类的方法定义通用的打字稿接口吗?

[英]Can I define a generic typescript Interface for the methods of a Class?

I'm cleaning up the types of my Angular 4 Application and I would like to define an interface to the Database classes in charge of the API calls. 我正在清理Angular 4应用程序的类型,并且想定义一个负责API调用的Database类的接口。

Those classes will have methods receiving Criteria objects, and returning Observables of ResultItems[] that are widely different. 这些类将具有接收Criteria对象并返回ResultItems[]很大的ResultItems[] Observables的ResultItems[] So I've tried to define this Interface: 所以我尝试定义此接口:

import { Observable } from 'rxjs/Observable';

export interface Database<C> {
  [methodName: string]: (args: C) => Observable<any>;
}

to cover the methods receiving a Criteria and returning any thing. 涵盖接收Criteria并返回any东西的方法。

Then I try to have this kind of implementation: 然后,我尝试进行这种实现:

@Injectable()
export class ItemsDatabase<ItemsCriteria> implements Database<ItemsCriteria> {
  constructor(private api: ApiService) {}

  fetch(args: ItemsCriteria): Observable<APIResponse[]> {
    return this.api.fetchItems(args);
  }

  summary(args: ItemsCriteria): Observable<SummaryResponse[]> {
    return this.api.itemsSummary(args);
  }
}

but Typescript denies it throwing: 但Typescript拒绝抛出:

Class 'ItemsDatabase<ItemsCriteria>' incorrectly implements interface 'Database<ItemsCriteria>'.
  Index signature is missing in type 'ItemsDatabase<ItemsCriteria>'

Any advice? 有什么建议吗? or this is just not possible? 还是这是不可能的? Thanks in advance! 提前致谢!

You have to add the index signature to the class 您必须将索引签名添加到类中

@Injectable()
export class ItemsDatabase<ItemsCriteria> implements Database<ItemsCriteria> {
  constructor(private api: ApiService) {}
  [methodName: string]: (args: ItemsCriteria) => Observable<any>;
  fetch(args: ItemsCriteria): Observable<APIResponse[]> {
    return this.api.fetchItems(args);
  }

  summary(args: ItemsCriteria): Observable<SummaryResponse[]> {
    return this.api.itemsSummary(args);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在一个类中定义一个特定的类类型,该类在打字稿中实现了一个具有泛型类型的接口吗? - Can I define a specific class type in a class that implements an interface with a generic type in typescript? 定义一个基于递归泛型接口的 TypeScript class - Define a TypeScript class based on recursive generic interface 带有泛型方法的 Typescript 接口 - Typescript interface with generic methods 通用方法的Typescript接口 - Typescript interface for generic methods 为什么我不能在打字稿类中定义接口或类型 - why i can't define interface or type inside typescript class 如何在导出的Typescript类中定义接口? - How can I define an interface in an exported Typescript class? 如何在Typescript中定义一个类,该类接受一个接口,该接口具有两个用于相同泛型的字段并保证它们是同一类型? - How do I define a class in Typescript that accepts an interface that has two fields for the same generic and guarantee they are the same type? 我可以定义一个带有计算键的 Typescript 接口吗? - Can I define a Typescript interface with computed keys? 如何在打字稿中定义匿名通用接口? - How do I define an anonymous generic interface in typescript? 如何基于类generic定义泛型接口 - how to define generic interface based on class generic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM