From 8166bd8941ffb3f8e94ba2e421ae0d8fcb25f974 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Tue, 21 Jan 2025 14:10:41 +0000 Subject: [PATCH] feat(collection): use @@species in static methods --- packages/collection/src/collection.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 288b2291cd19..3f699d844128 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -11,11 +11,11 @@ export type ReadonlyCollection = Omit< export interface Collection { /** - * Ambient declaration to allow `this.constructor[@@species]` in class methods. + * Ambient declaration to allow references to `this.constructor` in class methods. * * @internal */ - constructor: typeof Collection & { readonly [Symbol.species]: typeof Collection }; + constructor: typeof Collection; } /** @@ -1076,7 +1076,7 @@ export class Collection extends Map { entries: Iterable<[Key, Value]>, combine: (firstValue: Value, secondValue: Value, key: Key) => Value, ): Collection { - const coll = new Collection(); + const coll = new this[Symbol.species](); for (const [key, value] of entries) { if (coll.has(key)) { coll.set(key, combine(coll.get(key)!, value, key)); @@ -1087,6 +1087,11 @@ export class Collection extends Map { return coll; } + + /** + * @internal + */ + declare public static readonly [Symbol.species]: typeof Collection; } /**