我已经阅读并成功地使用以下代码将成员添加到数组接口:
interface Array<T> {
first(): T | undefined;
}
Array.prototype.first = function<T>(this: T[]): T | undefined {
return this.length > 0 ? this[0] : undefined;
};
但是,如果我尝试对iterable接口执行同样的操作,它将不起作用:
interface Iterable<T> {
first(): T | undefined;
}
Iterable.prototype.first = function <T>(this: Iterable<T>): T | undefined {
return undefined;
}
TS编译器返回一个关于我试图修改iterable原型的行的错误:
'Iterable' only refers to a type, but is being used as a value here.
是否可以向ITerable添加方法?谢谢!