Skip to content

Commit b1ea83a

Browse files
committed
add types for set methods proposal
1 parent 3251a12 commit b1ea83a

File tree

6 files changed

+854
-0
lines changed

6 files changed

+854
-0
lines changed

src/lib/esnext.collection.d.ts

+46
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,49 @@ interface MapConstructor {
99
keySelector: (item: T, index: number) => K,
1010
): Map<K, T[]>;
1111
}
12+
13+
interface SetLike<T> {
14+
/**
15+
* Despite its name, returns an iterable of the values in the set-like.
16+
*/
17+
keys(): Iterable<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(other: SetLike<T>): Set<T>;
33+
/**
34+
* @returns a new Set containing all the elements which are both in this Set and in the argument.
35+
*/
36+
intersection(other: SetLike<T>): Set<T>;
37+
/**
38+
* @returns a new Set containing all the elements in this Set which are not also in the argument.
39+
*/
40+
difference(other: SetLike<T>): Set<T>;
41+
/**
42+
* @returns a new Set containing all the elements in this Set which are in this or in the argument, but not in both.
43+
*/
44+
symmetricDifference(other: SetLike<T>): Set<T>;
45+
/**
46+
* @returns a boolean indicating whether all the elements in this Set are also in the argument.
47+
*/
48+
isSubsetOf(other: SetLike<T>): Set<T>;
49+
/**
50+
* @returns a boolean indicating whether all the elements in the argument are also in this Set.
51+
*/
52+
isSupersetOf(other: SetLike<T>): Set<T>;
53+
/**
54+
* @returns a boolean indicating whether this Set has no elements in common with the argument.
55+
*/
56+
isDisjointFrom(other: SetLike<T>): Set<T>;
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
setMethods.ts(13,17): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
2+
Type 'undefined[]' is missing the following properties from type 'SetLike<number>': has, size
3+
setMethods.ts(15,17): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
4+
The types returned by 'keys()[Symbol.iterator]().next(...)' are incompatible between these types.
5+
Type 'IteratorResult<string, any>' is not assignable to type 'IteratorResult<number, any>'.
6+
Type 'IteratorYieldResult<string>' is not assignable to type 'IteratorResult<number, any>'.
7+
Type 'IteratorYieldResult<string>' is not assignable to type 'IteratorYieldResult<number>'.
8+
Type 'string' is not assignable to type 'number'.
9+
setMethods.ts(19,24): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
10+
setMethods.ts(21,24): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
11+
setMethods.ts(25,22): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
12+
setMethods.ts(27,22): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
13+
setMethods.ts(31,31): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
14+
setMethods.ts(33,31): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
15+
setMethods.ts(37,22): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
16+
setMethods.ts(39,22): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
17+
setMethods.ts(43,24): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
18+
setMethods.ts(45,24): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
19+
setMethods.ts(49,26): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
20+
setMethods.ts(51,26): error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
21+
22+
23+
==== setMethods.ts (14 errors) ====
24+
let numberSet = new Set([0, 1, 2]);
25+
26+
let stringSet = new Set(["a", "b"]);
27+
28+
let numberMap = new Map([[4, {}], [5, {}]]);
29+
30+
let numberSetLike = {
31+
size: 1,
32+
*keys() { yield 3 },
33+
has(x) { return x === 3 },
34+
};
35+
36+
numberSet.union([]);
37+
~~
38+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
39+
!!! error TS2345: Type 'undefined[]' is missing the following properties from type 'SetLike<number>': has, size
40+
numberSet.union(new Set);
41+
numberSet.union(stringSet);
42+
~~~~~~~~~
43+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
44+
!!! error TS2345: The types returned by 'keys()[Symbol.iterator]().next(...)' are incompatible between these types.
45+
!!! error TS2345: Type 'IteratorResult<string, any>' is not assignable to type 'IteratorResult<number, any>'.
46+
!!! error TS2345: Type 'IteratorYieldResult<string>' is not assignable to type 'IteratorResult<number, any>'.
47+
!!! error TS2345: Type 'IteratorYieldResult<string>' is not assignable to type 'IteratorYieldResult<number>'.
48+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
49+
numberSet.union(numberMap);
50+
numberSet.union(numberSetLike);
51+
52+
numberSet.intersection([]);
53+
~~
54+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
55+
numberSet.intersection(new Set);
56+
numberSet.intersection(stringSet);
57+
~~~~~~~~~
58+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
59+
numberSet.intersection(numberMap);
60+
numberSet.intersection(numberSetLike);
61+
62+
numberSet.difference([]);
63+
~~
64+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
65+
numberSet.difference(new Set);
66+
numberSet.difference(stringSet);
67+
~~~~~~~~~
68+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
69+
numberSet.difference(numberMap);
70+
numberSet.difference(numberSetLike);
71+
72+
numberSet.symmetricDifference([]);
73+
~~
74+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
75+
numberSet.symmetricDifference(new Set);
76+
numberSet.symmetricDifference(stringSet);
77+
~~~~~~~~~
78+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
79+
numberSet.symmetricDifference(numberMap);
80+
numberSet.symmetricDifference(numberSetLike);
81+
82+
numberSet.isSubsetOf([]);
83+
~~
84+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
85+
numberSet.isSubsetOf(new Set);
86+
numberSet.isSubsetOf(stringSet);
87+
~~~~~~~~~
88+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
89+
numberSet.isSubsetOf(numberMap);
90+
numberSet.isSubsetOf(numberSetLike);
91+
92+
numberSet.isSupersetOf([]);
93+
~~
94+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
95+
numberSet.isSupersetOf(new Set);
96+
numberSet.isSupersetOf(stringSet);
97+
~~~~~~~~~
98+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
99+
numberSet.isSupersetOf(numberMap);
100+
numberSet.isSupersetOf(numberSetLike);
101+
102+
numberSet.isDisjointFrom([]);
103+
~~
104+
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'SetLike<number>'.
105+
numberSet.isDisjointFrom(new Set);
106+
numberSet.isDisjointFrom(stringSet);
107+
~~~~~~~~~
108+
!!! error TS2345: Argument of type 'Set<string>' is not assignable to parameter of type 'SetLike<number>'.
109+
numberSet.isDisjointFrom(numberMap);
110+
numberSet.isDisjointFrom(numberSetLike);
111+
+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)