Skip to content

Commit e8274f7

Browse files
authored
add types for set methods proposal (#57230)
1 parent 4da470d commit e8274f7

12 files changed

+1083
-12
lines changed

src/lib/esnext.collection.d.ts

+77
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,80 @@ interface MapConstructor {
99
keySelector: (item: T, index: number) => K,
1010
): Map<K, T[]>;
1111
}
12+
13+
interface ReadonlySetLike<T> {
14+
/**
15+
* Despite its name, returns an iterator of the values in the set-like.
16+
*/
17+
keys(): Iterator<T>;
18+
/**
19+
* @returns a boolean indicating whether an element with the specified value exists in the set-like or not.
20+
*/
21+
has(value: T): boolean;
22+
/**
23+
* @returns the number of (unique) elements in the set-like.
24+
*/
25+
readonly size: number;
26+
}
27+
28+
interface Set<T> {
29+
/**
30+
* @returns a new Set containing all the elements in this Set and also all the elements in the argument.
31+
*/
32+
union<U>(other: ReadonlySetLike<U>): Set<T | U>;
33+
/**
34+
* @returns a new Set containing all the elements which are both in this Set and in the argument.
35+
*/
36+
intersection<U>(other: ReadonlySetLike<U>): Set<T & U>;
37+
/**
38+
* @returns a new Set containing all the elements in this Set which are not also in the argument.
39+
*/
40+
difference<U>(other: ReadonlySetLike<U>): Set<T>;
41+
/**
42+
* @returns a new Set containing all the elements which are in either this Set or in the argument, but not in both.
43+
*/
44+
symmetricDifference<U>(other: ReadonlySetLike<U>): Set<T | U>;
45+
/**
46+
* @returns a boolean indicating whether all the elements in this Set are also in the argument.
47+
*/
48+
isSubsetOf(other: ReadonlySetLike<unknown>): boolean;
49+
/**
50+
* @returns a boolean indicating whether all the elements in the argument are also in this Set.
51+
*/
52+
isSupersetOf(other: ReadonlySetLike<unknown>): boolean;
53+
/**
54+
* @returns a boolean indicating whether this Set has no elements in common with the argument.
55+
*/
56+
isDisjointFrom(other: ReadonlySetLike<unknown>): boolean;
57+
}
58+
59+
interface ReadonlySet<T> {
60+
/**
61+
* @returns a new Set containing all the elements in this Set and also all the elements in the argument.
62+
*/
63+
union<U>(other: ReadonlySetLike<U>): Set<T | U>;
64+
/**
65+
* @returns a new Set containing all the elements which are both in this Set and in the argument.
66+
*/
67+
intersection<U>(other: ReadonlySetLike<U>): Set<T & U>;
68+
/**
69+
* @returns a new Set containing all the elements in this Set which are not also in the argument.
70+
*/
71+
difference<U>(other: ReadonlySetLike<U>): Set<T>;
72+
/**
73+
* @returns a new Set containing all the elements which are in either this Set or in the argument, but not in both.
74+
*/
75+
symmetricDifference<U>(other: ReadonlySetLike<U>): Set<T | U>;
76+
/**
77+
* @returns a boolean indicating whether all the elements in this Set are also in the argument.
78+
*/
79+
isSubsetOf(other: ReadonlySetLike<unknown>): boolean;
80+
/**
81+
* @returns a boolean indicating whether all the elements in the argument are also in this Set.
82+
*/
83+
isSupersetOf(other: ReadonlySetLike<unknown>): boolean;
84+
/**
85+
* @returns a boolean indicating whether this Set has no elements in common with the argument.
86+
*/
87+
isDisjointFrom(other: ReadonlySetLike<unknown>): boolean;
88+
}

tests/baselines/reference/classExtendingAbstractClassWithMemberCalledTheSameAsItsOwnTypeParam.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export abstract class BaseObservable<T, TChange = void> extends ConvenientObserv
6969

7070
protected readonly observers = new Set<IObserver>();
7171
>observers : Symbol(BaseObservable.observers, Decl(classExtendingAbstractClassWithMemberCalledTheSameAsItsOwnTypeParam.ts, 18, 98))
72-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
72+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
7373
>IObserver : Symbol(IObserver, Decl(classExtendingAbstractClassWithMemberCalledTheSameAsItsOwnTypeParam.ts, 0, 0))
7474
}
7575

tests/baselines/reference/esNextWeakRefs_IterableWeakMap.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const IterableWeakMap_cleanup = ({ ref, set }: {
1313

1414
readonly set: Set<WeakRef<object>>;
1515
>set : Symbol(set, Decl(esNextWeakRefs_IterableWeakMap.ts, 2, 34))
16-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
16+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
1717
>WeakRef : Symbol(WeakRef, Decl(lib.es2021.weakref.d.ts, --, --), Decl(lib.es2021.weakref.d.ts, --, --))
1818

1919
}) => {
@@ -52,7 +52,7 @@ export class IterableWeakMap<K extends object, V> implements WeakMap<K, V> {
5252

5353
#refSet = new Set<WeakRef<K>>();
5454
>#refSet : Symbol(IterableWeakMap.#refSet, Decl(esNextWeakRefs_IterableWeakMap.ts, 12, 72))
55-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
55+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
5656
>WeakRef : Symbol(WeakRef, Decl(lib.es2021.weakref.d.ts, --, --), Decl(lib.es2021.weakref.d.ts, --, --))
5757
>K : Symbol(K, Decl(esNextWeakRefs_IterableWeakMap.ts, 9, 29))
5858

tests/baselines/reference/extendsTag1.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
class My extends Set {}
99
>My : Symbol(My, Decl(bug25101.js, 0, 0))
10-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
10+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
1111

tests/baselines/reference/mapGroupBy.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type Employee = { name: string, role: 'ic' | 'manager' }
2424

2525
const employees: Set<Employee> = new Set();
2626
>employees : Symbol(employees, Decl(mapGroupBy.ts, 5, 5))
27-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
27+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
2828
>Employee : Symbol(Employee, Decl(mapGroupBy.ts, 2, 46))
29-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
29+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
3030

3131
const byRole = Map.groupBy(employees, x => x.role);
3232
>byRole : Symbol(byRole, Decl(mapGroupBy.ts, 6, 5))

tests/baselines/reference/objectGroupBy.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type Employee = { name: string, role: 'ic' | 'manager' }
2424

2525
const employees: Set<Employee> = new Set();
2626
>employees : Symbol(employees, Decl(objectGroupBy.ts, 5, 5))
27-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
27+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
2828
>Employee : Symbol(Employee, Decl(objectGroupBy.ts, 2, 49))
29-
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
29+
>Set : Symbol(Set, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.collection.d.ts, --, --))
3030

3131
const byRole = Object.groupBy(employees, x => x.role);
3232
>byRole : Symbol(byRole, Decl(objectGroupBy.ts, 6, 5))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
setMethods.ts(13,17): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
2+
Type 'undefined[]' is missing the following properties from type 'ReadonlySetLike<unknown>': has, size
3+
setMethods.ts(19,24): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
4+
setMethods.ts(25,22): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
5+
setMethods.ts(31,31): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
6+
setMethods.ts(37,22): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
7+
setMethods.ts(43,24): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
8+
setMethods.ts(49,26): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
9+
10+
11+
==== setMethods.ts (7 errors) ====
12+
let numberSet = new Set([0, 1, 2]);
13+
14+
let stringSet = new Set(["a", "b"]);
15+
16+
let numberMap = new Map([[4, {}], [5, {}]]);
17+
18+
let numberSetLike = {
19+
size: 1,
20+
*keys() { yield 3 },
21+
has(x) { return x === 3 },
22+
};
23+
24+
numberSet.union([]);
25+
~~
26+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
27+
!!! error TS2345: Type 'undefined[]' is missing the following properties from type 'ReadonlySetLike<unknown>': has, size
28+
numberSet.union(new Set);
29+
numberSet.union(stringSet);
30+
numberSet.union(numberMap);
31+
numberSet.union(numberSetLike);
32+
33+
numberSet.intersection([]);
34+
~~
35+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
36+
numberSet.intersection(new Set);
37+
numberSet.intersection(stringSet);
38+
numberSet.intersection(numberMap);
39+
numberSet.intersection(numberSetLike);
40+
41+
numberSet.difference([]);
42+
~~
43+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
44+
numberSet.difference(new Set);
45+
numberSet.difference(stringSet);
46+
numberSet.difference(numberMap);
47+
numberSet.difference(numberSetLike);
48+
49+
numberSet.symmetricDifference([]);
50+
~~
51+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
52+
numberSet.symmetricDifference(new Set);
53+
numberSet.symmetricDifference(stringSet);
54+
numberSet.symmetricDifference(numberMap);
55+
numberSet.symmetricDifference(numberSetLike);
56+
57+
numberSet.isSubsetOf([]);
58+
~~
59+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
60+
numberSet.isSubsetOf(new Set);
61+
numberSet.isSubsetOf(stringSet);
62+
numberSet.isSubsetOf(numberMap);
63+
numberSet.isSubsetOf(numberSetLike);
64+
65+
numberSet.isSupersetOf([]);
66+
~~
67+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
68+
numberSet.isSupersetOf(new Set);
69+
numberSet.isSupersetOf(stringSet);
70+
numberSet.isSupersetOf(numberMap);
71+
numberSet.isSupersetOf(numberSetLike);
72+
73+
numberSet.isDisjointFrom([]);
74+
~~
75+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'.
76+
numberSet.isDisjointFrom(new Set);
77+
numberSet.isDisjointFrom(stringSet);
78+
numberSet.isDisjointFrom(numberMap);
79+
numberSet.isDisjointFrom(numberSetLike);
80+
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//// [tests/cases/compiler/setMethods.ts] ////
2+
3+
//// [setMethods.ts]
4+
let numberSet = new Set([0, 1, 2]);
5+
6+
let stringSet = new Set(["a", "b"]);
7+
8+
let numberMap = new Map([[4, {}], [5, {}]]);
9+
10+
let numberSetLike = {
11+
size: 1,
12+
*keys() { yield 3 },
13+
has(x) { return x === 3 },
14+
};
15+
16+
numberSet.union([]);
17+
numberSet.union(new Set);
18+
numberSet.union(stringSet);
19+
numberSet.union(numberMap);
20+
numberSet.union(numberSetLike);
21+
22+
numberSet.intersection([]);
23+
numberSet.intersection(new Set);
24+
numberSet.intersection(stringSet);
25+
numberSet.intersection(numberMap);
26+
numberSet.intersection(numberSetLike);
27+
28+
numberSet.difference([]);
29+
numberSet.difference(new Set);
30+
numberSet.difference(stringSet);
31+
numberSet.difference(numberMap);
32+
numberSet.difference(numberSetLike);
33+
34+
numberSet.symmetricDifference([]);
35+
numberSet.symmetricDifference(new Set);
36+
numberSet.symmetricDifference(stringSet);
37+
numberSet.symmetricDifference(numberMap);
38+
numberSet.symmetricDifference(numberSetLike);
39+
40+
numberSet.isSubsetOf([]);
41+
numberSet.isSubsetOf(new Set);
42+
numberSet.isSubsetOf(stringSet);
43+
numberSet.isSubsetOf(numberMap);
44+
numberSet.isSubsetOf(numberSetLike);
45+
46+
numberSet.isSupersetOf([]);
47+
numberSet.isSupersetOf(new Set);
48+
numberSet.isSupersetOf(stringSet);
49+
numberSet.isSupersetOf(numberMap);
50+
numberSet.isSupersetOf(numberSetLike);
51+
52+
numberSet.isDisjointFrom([]);
53+
numberSet.isDisjointFrom(new Set);
54+
numberSet.isDisjointFrom(stringSet);
55+
numberSet.isDisjointFrom(numberMap);
56+
numberSet.isDisjointFrom(numberSetLike);
57+
58+
59+
//// [setMethods.js]
60+
let numberSet = new Set([0, 1, 2]);
61+
let stringSet = new Set(["a", "b"]);
62+
let numberMap = new Map([[4, {}], [5, {}]]);
63+
let numberSetLike = {
64+
size: 1,
65+
*keys() { yield 3; },
66+
has(x) { return x === 3; },
67+
};
68+
numberSet.union([]);
69+
numberSet.union(new Set);
70+
numberSet.union(stringSet);
71+
numberSet.union(numberMap);
72+
numberSet.union(numberSetLike);
73+
numberSet.intersection([]);
74+
numberSet.intersection(new Set);
75+
numberSet.intersection(stringSet);
76+
numberSet.intersection(numberMap);
77+
numberSet.intersection(numberSetLike);
78+
numberSet.difference([]);
79+
numberSet.difference(new Set);
80+
numberSet.difference(stringSet);
81+
numberSet.difference(numberMap);
82+
numberSet.difference(numberSetLike);
83+
numberSet.symmetricDifference([]);
84+
numberSet.symmetricDifference(new Set);
85+
numberSet.symmetricDifference(stringSet);
86+
numberSet.symmetricDifference(numberMap);
87+
numberSet.symmetricDifference(numberSetLike);
88+
numberSet.isSubsetOf([]);
89+
numberSet.isSubsetOf(new Set);
90+
numberSet.isSubsetOf(stringSet);
91+
numberSet.isSubsetOf(numberMap);
92+
numberSet.isSubsetOf(numberSetLike);
93+
numberSet.isSupersetOf([]);
94+
numberSet.isSupersetOf(new Set);
95+
numberSet.isSupersetOf(stringSet);
96+
numberSet.isSupersetOf(numberMap);
97+
numberSet.isSupersetOf(numberSetLike);
98+
numberSet.isDisjointFrom([]);
99+
numberSet.isDisjointFrom(new Set);
100+
numberSet.isDisjointFrom(stringSet);
101+
numberSet.isDisjointFrom(numberMap);
102+
numberSet.isDisjointFrom(numberSetLike);

0 commit comments

Comments
 (0)