Javascript 使用';这';参数

Javascript 使用';这';参数,javascript,arrays,typescript,generics,this,Javascript,Arrays,Typescript,Generics,This,我有一个Angular 8/TS应用程序,我想在Array的原型中添加一个.groupBy()函数。我已经阅读了TypeScript,它声明您只需将this:作为伪第一个参数添加到您的参数中 当我为原型定义它时,它在方法内部工作得非常出色,但是无论我在哪里调用该方法,我的数组都会恢复为类型any 所以我的问题是,我想,我使用的是一个数组(它是泛型的),我的函数采用泛型参数,如下所示: if(!Array.prototype.groupBy) { Array.prototype.group

我有一个Angular 8/TS应用程序,我想在Array的原型中添加一个
.groupBy()
函数。我已经阅读了TypeScript,它声明您只需将
this:
作为伪第一个参数添加到您的参数中

当我为原型定义它时,它在方法内部工作得非常出色,但是无论我在哪里调用该方法,我的数组都会恢复为类型
any

所以我的问题是,我想,我使用的是一个数组(它是泛型的),我的函数采用泛型参数,如下所示:

if(!Array.prototype.groupBy) {
    Array.prototype.groupBy = function<T, TKey, TResult>(
        this: T[], // here's the magical "this" parameter
        keySelector: (item: T) => TKey,
        itemSelector: (item: T) => TResult
    ): Group<TResult, TKey>[] {
如果我在调用
arr.groupBy(..)
时是显式的,那么它可以用于查找,但我不想显式地声明泛型类型如何编写“扩展方法”,使其不必显式地声明泛型类型?

谢谢

为了完整起见,下面是我完整的groupBy函数:

if(!Array.prototype.groupBy) {
    Array.prototype.groupBy = function<T, TKey, TResult>(
        this: T[],
        keySelector: (item: T) => TKey,
        itemSelector: (item: T) => TResult
    ): Group<TResult, TKey>[] {
        let groupDict: any = {};

        // HERE, if I provide the "this parameter", then "this" is an Array<T>. If I don't,
        // then "this" is an "any". So the "this parameter" is working just fine inside the
        // function declaration.
        this.forEach((item: T) => {
            let groupKey = keySelector(item);

            let group = groupDict[groupKey];
            if (!group) {
                group = new Group<TResult, TKey>(groupKey);
                groupDict[groupKey] = group;
            }

            group.members.push(itemSelector(item))
        });

        return Object.keys(groupDict)
            .map(key => <Group<TResult, TKey>>groupDict[key]);
    }
}
if(!Array.prototype.groupBy){
Array.prototype.groupBy=函数(
this:T[],
按键选择器:(项目:T)=>TKey,
项目选择器:(项目:T)=>TResult
):组[]{
设groupDict:any={};
//这里,如果我提供“this参数”,那么“this”是一个数组。如果我不提供,
//那么“this”就是“any”,所以“this参数”在
//函数声明。
此.forEach((项目:T)=>{
让groupKey=keySelector(项);
设group=groupDict[groupKey];
如果(!组){
组=新组(groupKey);
groupDict[groupKey]=组;
}
group.members.push(项选择器(项))
});
返回Object.keys(groupDict)
.map(key=>groupDict[key]);
}
}

我刚刚查看了VSO中的
Array.prototype.concat
函数。智能感知告诉我:
Array.concat(…项:ConcatArray[]):any[]
。看起来默认数组函数对其泛型使用了
。你认为你可以从你的自定义函数中删除泛型吗?对不起,为了让它工作,我升级到Angular 9,它破坏了很多东西。尽管如此,这只是一个个人项目,我正在慢慢地比以往更好地重建它。我到那里后会试试这个。
if(!Array.prototype.groupBy) {
    Array.prototype.groupBy = function<T, TKey, TResult>(
        this: T[],
        keySelector: (item: T) => TKey,
        itemSelector: (item: T) => TResult
    ): Group<TResult, TKey>[] {
        let groupDict: any = {};

        // HERE, if I provide the "this parameter", then "this" is an Array<T>. If I don't,
        // then "this" is an "any". So the "this parameter" is working just fine inside the
        // function declaration.
        this.forEach((item: T) => {
            let groupKey = keySelector(item);

            let group = groupDict[groupKey];
            if (!group) {
                group = new Group<TResult, TKey>(groupKey);
                groupDict[groupKey] = group;
            }

            group.members.push(itemSelector(item))
        });

        return Object.keys(groupDict)
            .map(key => <Group<TResult, TKey>>groupDict[key]);
    }
}