Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

types: union value types on intersect and difference #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export interface Collection<K, V> extends Map<K, V> {
constructor: CollectionConstructor;
}

/**
* A utility type for merging type of collections with two seperate value types.
*
* @internal
*/
export type ValueMerged<K, V, C2 extends Collection<K, unknown>> = C2 extends Collection<K, infer O>
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
? Collection<K, O | V>
: 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.
Expand Down Expand Up @@ -629,6 +638,7 @@ export class Collection<K, V> extends Map<K, V> {
*
* @param other The other Collection to filter against
*/
public intersect<C extends Collection<K, unknown>>(other: C): ValueMerged<K, V, C>;
public intersect(other: Collection<K, V>) {
const coll = new this.constructor[Symbol.species]<K, V>();
for (const [k, v] of other) {
Expand All @@ -642,6 +652,7 @@ export class Collection<K, V> extends Map<K, V> {
*
* @param other The other Collection to filter against
*/
public difference<C extends Collection<K, unknown>>(other: C): ValueMerged<K, V, C>;
public difference(other: Collection<K, V>) {
const coll = new this.constructor[Symbol.species]<K, V>();
for (const [k, v] of other) {
Expand Down