@@ -9,6 +9,12 @@ export interface CollectionConstructor {
9
9
readonly [ Symbol . species ] : CollectionConstructor ;
10
10
}
11
11
12
+ /**
13
+ * Represents an immutable version of a collection
14
+ */
15
+ export type ReadonlyCollection < K , V > = ReadonlyMap < K , V > &
16
+ Omit < Collection < K , V > , 'forEach' | 'ensure' | 'reverse' | 'sweep' | 'sort' | 'get' | 'set' | 'delete' > ;
17
+
12
18
/**
13
19
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
14
20
*
@@ -562,7 +568,7 @@ export class Collection<K, V> extends Map<K, V> {
562
568
* @example
563
569
* const newColl = someColl.clone();
564
570
*/
565
- public clone ( ) {
571
+ public clone ( ) : Collection < K , V > {
566
572
return new this . constructor [ Symbol . species ] ( this ) ;
567
573
}
568
574
@@ -574,7 +580,7 @@ export class Collection<K, V> extends Map<K, V> {
574
580
* @example
575
581
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
576
582
*/
577
- public concat ( ...collections : Collection < K , V > [ ] ) {
583
+ public concat ( ...collections : ReadonlyCollection < K , V > [ ] ) {
578
584
const newColl = this . clone ( ) ;
579
585
for ( const coll of collections ) {
580
586
for ( const [ key , val ] of coll ) newColl . set ( key , val ) ;
@@ -591,7 +597,7 @@ export class Collection<K, V> extends Map<K, V> {
591
597
*
592
598
* @returns Whether the collections have identical contents
593
599
*/
594
- public equals ( collection : Collection < K , V > ) {
600
+ public equals ( collection : ReadonlyCollection < K , V > ) {
595
601
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
596
602
if ( ! collection ) return false ; // runtime check
597
603
if ( this === collection ) return true ;
@@ -634,7 +640,7 @@ export class Collection<K, V> extends Map<K, V> {
634
640
*
635
641
* @param other The other Collection to filter against
636
642
*/
637
- public intersect < T > ( other : Collection < K , T > ) : Collection < K , T > {
643
+ public intersect < T > ( other : ReadonlyCollection < K , T > ) : Collection < K , T > {
638
644
const coll = new this . constructor [ Symbol . species ] < K , T > ( ) ;
639
645
for ( const [ k , v ] of other ) {
640
646
if ( this . has ( k ) && Object . is ( v , this . get ( k ) ) ) {
@@ -649,7 +655,7 @@ export class Collection<K, V> extends Map<K, V> {
649
655
*
650
656
* @param other The other Collection to filter against
651
657
*/
652
- public difference < T > ( other : Collection < K , T > ) : Collection < K , V | T > {
658
+ public difference < T > ( other : ReadonlyCollection < K , T > ) : Collection < K , V | T > {
653
659
const coll = new this . constructor [ Symbol . species ] < K , V | T > ( ) ;
654
660
for ( const [ k , v ] of other ) {
655
661
if ( ! this . has ( k ) ) coll . set ( k , v ) ;
0 commit comments