我正在尝试用一些方法定义一个接口(interface),我希望其中一个方法是通用的。
它是一个filterUnique
方法,所以它应该能够过滤数字、字符串等列表。
以下不适合我:
export interface IGenericServices {
filterUnique(array: Array<T>): Array<T>;
}
有没有办法进行编译,还是我在这里的某个地方犯了概念性错误?
干杯!
最佳答案
T
类型尚未定义。它需要作为类型变量添加到方法中,例如:
filterUnique<T>(array: Array<T>): Array<T>;
或者像这样添加到界面中:
export interface IGenericServices<T> {
filterUnique(array: Array<T>): Array<T>;
}
关于generics - 接口(interface)中的 TypeScript 通用方法签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704645/