I am trying to define an interface with a few methods, and I would like one of the methods to be generic.
我试图用几个方法定义一个接口,我希望其中一个方法是通用的。
It is a filterUnique
method, so it should be able to filter lists of numbers, strings, etc.
它是一个filterUnique方法,因此它应该能够过滤数字,字符串等列表。
the following does not compile for me:
以下内容不适合我:
export interface IGenericServices {
filterUnique(array: Array<T>): Array<T>;
}
Is there a way to make this compile, or am I making a conceptual mistake somewhere here?
有没有办法进行编译,或者我在这里某处犯了概念性错误?
Cheers!
干杯!
1 个解决方案
#1
16
The T
type isn't defined yet. It needs to be added to the method as a type variable like:
T类型尚未定义。它需要作为类型变量添加到方法中,如:
filterUnique<T>(array: Array<T>): Array<T>;
Or added to the interface like:
或者添加到界面中:
export interface IGenericServices<T> {
filterUnique(array: Array<T>): Array<T>;
}
#1
16
The T
type isn't defined yet. It needs to be added to the method as a type variable like:
T类型尚未定义。它需要作为类型变量添加到方法中,如:
filterUnique<T>(array: Array<T>): Array<T>;
Or added to the interface like:
或者添加到界面中:
export interface IGenericServices<T> {
filterUnique(array: Array<T>): Array<T>;
}