所以我試圖使這個作業。Array<T>.groupBy<KeyType>(屬性):{key: KeyType, array: Array<T> }[];。
這段代碼看起來像這樣:
type ArrayByParameter< T, KeyType = any> = string | ((item: T)=> KeyType)。)
宣告 global {
介面 Array<T> {
groupBy<KeyType = string>(
property: ArrayByParameter<T,KeyType>
): { key: KeyType; array: T[] }[];
}
}
if (!Array.>prototype.groupBy) {
Array.prototype。 groupBy = function <KeyType = string> (
property: ArrayByParameter<any, KeyType>
) {
let callbackFunction: (item: any) => any;
if (typeof property === "string") {
callbackFunction = (mapObj) => mapObj[屬性]。
} else if (typeof property =="function") {
callbackFunction = property。
} else {
throw "Parameter is not a string nor a function!" ;
}
// Edit version of : https://stackoverflow.com/a/34890276/3781156
return Object.entries(
this.reduce(function (rv, x) {
(rv[callbackFunction(x)] = rv[callbackFunction(x)] || []).push (
x
);
return rv;
}, {}) as { [key: string] 。Array<any> }。
).map(([key, array]) =>/span> {
return { key, array };
});
};
}
type Characters = "A"/span> | "B"/span> | "C"/span>;
型別 Numbers = "1"/span> | "2"/span> | "3"/span>。
type SomeKeyType = `${Characters}-${Numbers}`。
//和下面一樣。
型別 SomeKeyType2 =
| "A-1"/span>
| "A-2"/span>
| "A-3" | "A-3"
| "B-1""B-2" | "B-2"
| "B-3""C-1""C-2" "C-2"
| "C-3"。
型別 SomeObject = {
c: Characters;
n: 數字。
/.../span>
};
const array: SomeObject[] = [
{ c: "A"/span>, n: 1 },
{ c: "A"/span>, n: 2 },
{ c: "A"/span>, n: 2 },
{ c: "B"/span>, n: 1 },
// ...
];
const groupByArray: { key。SomeKeyType; array: SomeObject[] }[] =
陣列。 groupBy<KeyType>((obj: SomeObject) => `${obj. c}-${obj.n}`)。)
Result 預期。
[
{key: "A-1", array: [{c:A, n:1, /*。 ...*/}]}。
{key:"A-2", array: [{/*...*/},{/*...}]},
{key:"B-1", array:[{/*...*/}},
/*...*/].
];
我在第12行的 "Array.prototype.groupBy "處得到這樣的錯誤:
型別'<KeyType = string>(property: ArrayByParameter<any, KeyType>) => { key: string; array: any[]; }[]'不能分配給型別'<KeyType = string>(property: ArrayByParameter<any, KeyType>) => { key: KeyType; array: any[]; }[]'。 型別'{ key: string; array: any[]; }[]'不能被分配給型別'{ key: KeyType; array: any[]; }[]'。 型別'{ key: string; array: any[]; }'不能被分配到型別'{ key: keyType; array: any[]; }'。 屬性'key'的型別不兼容。 型別'string'不能被分配到型別'KeyType'。 'KeyType'可以用一個任意的型別進行實體化,這個型別可能與'string'無關。
我認為我的 KeyType 定義是問題所在,但我無法找到解決辦法。KeyType是一個字串,但也可以是一個模板字面型別,這就是我現在想要的。
所以 :
所以:
我想知道
- 有辦法在Array.prototype.groupBy函式中獲得T泛型嗎?
- 現在,T被替換為任意,因為我不知道如何在原型定義中使用T。
提前感謝! :)
uj5u.com熱心網友回復:
我怎樣才能解決這個問題,使KeyType匹配?
這里的關鍵問題是,Object. entries目前是硬編碼的key索引器到string,而不是允許K extends PropertyKey(PropertyKey是一個內置的型別別名,在JavaScript中可以索引到物件的所有型別)。
沒有什么是我們不能用一個額外的環境型別定義來解決的:
。
declare module "our- global-ambient-module"{
global {
介面 Array<T> {
groupBy<KeyType extends PropertyKey = string> (
property: ArrayByParameter<T, KeyType>
): { key: KeyType; array: T[] }[];
}
//我們添加了什么 - `Object. entries`的多載。
//,這將保留鍵的型別。
介面 ObjectConstructor {
entries<K extends PropertyKey, V>(object: any)。[K, V][]。
}
}
完整的代碼在這里玩耍
。是否有辦法在Array.prototype.groupBy函式中獲得T作為一個泛型
?
是的,只要添加一個型別引數和型別this,就可以成為該型別:
Array.prototype。 groupBy = function < T, KeyType extends PropertyKey = string> (
this: T[],
property: ArrayByParameter<any, KeyType>
)
你還需要在你的reduce呼叫中對{}的型別進行注釋:
{}。as {[X in KeyType】。] T[]}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318598.html
標籤:
下一篇:型別腳本類約束字串
