From 34c4a7996487aeb5c9ce28208e4f2815c48b7d1c Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sun, 2 Jan 2022 14:51:17 -0500 Subject: [PATCH 1/3] types: union types on `intersect` and `difference` when value types are different --- src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/index.ts b/src/index.ts index 20d9fdf..35f3721 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,15 @@ export interface Collection extends Map { constructor: CollectionConstructor; } +/** + * A utility type for merging type of collections with two seperate value types. + * + * @internal + */ +export type ValueMerged> = C2 extends Collection + ? Collection + : never; + /** * A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has * an ID, for significantly improved performance and ease-of-use. @@ -629,6 +638,7 @@ export class Collection extends Map { * * @param other The other Collection to filter against */ + public intersect>(other: C): ValueMerged; public intersect(other: Collection) { const coll = new this.constructor[Symbol.species](); for (const [k, v] of other) { @@ -642,6 +652,7 @@ export class Collection extends Map { * * @param other The other Collection to filter against */ + public difference>(other: C): ValueMerged; public difference(other: Collection) { const coll = new this.constructor[Symbol.species](); for (const [k, v] of other) { From a0de811db4897e3263227c9b8accf852f43e0013 Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sun, 2 Jan 2022 15:27:41 -0500 Subject: [PATCH 2/3] types: remove utility type and overloads --- src/index.ts | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 35f3721..3df2d21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,15 +18,6 @@ export interface Collection extends Map { constructor: CollectionConstructor; } -/** - * A utility type for merging type of collections with two seperate value types. - * - * @internal - */ -export type ValueMerged> = C2 extends Collection - ? Collection - : never; - /** * A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has * an ID, for significantly improved performance and ease-of-use. @@ -638,9 +629,8 @@ export class Collection extends Map { * * @param other The other Collection to filter against */ - public intersect>(other: C): ValueMerged; - public intersect(other: Collection) { - const coll = new this.constructor[Symbol.species](); + public intersect(other: Collection): Collection { + const coll = new this.constructor[Symbol.species](); for (const [k, v] of other) { if (this.has(k)) coll.set(k, v); } @@ -652,9 +642,8 @@ export class Collection extends Map { * * @param other The other Collection to filter against */ - public difference>(other: C): ValueMerged; - public difference(other: Collection) { - const coll = new this.constructor[Symbol.species](); + public difference(other: Collection): Collection { + const coll = new this.constructor[Symbol.species](); for (const [k, v] of other) { if (!this.has(k)) coll.set(k, v); } From 962d6efe10f3b810ce623ec51bf6f11706d5a9a3 Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sun, 2 Jan 2022 22:41:37 -0500 Subject: [PATCH 3/3] fix: intersect shouldn't merge value types --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3df2d21..1ba5128 100644 --- a/src/index.ts +++ b/src/index.ts @@ -629,8 +629,8 @@ export class Collection extends Map { * * @param other The other Collection to filter against */ - public intersect(other: Collection): Collection { - const coll = new this.constructor[Symbol.species](); + public intersect(other: Collection): Collection { + const coll = new this.constructor[Symbol.species](); for (const [k, v] of other) { if (this.has(k)) coll.set(k, v); }