Skip to content

Commit 673fa41

Browse files
committed
Fix 8407: Allow (weak)map & (weak)set constructor when targeting ES5 using ES6 library (microsoft#8451)
* Move iterabl related stuffs off from es2015.collection * Move symbol.wellknown stuffs off from es2015.collection * Add map & set constructor support when targeting ES5 * Address PR: move symbol.iterator to es2015.iterable * Move Symbol.Iterator into es2015.iterable * Update baselines
1 parent 674e84b commit 673fa41

File tree

79 files changed

+245
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+245
-229
lines changed

src/lib/es2015.collection.d.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface Map<K, V> {
1010

1111
interface MapConstructor {
1212
new (): Map<any, any>;
13-
new <K, V>(): Map<K, V>;
13+
new <K, V>(entries?: [K, V][]): Map<K, V>;
1414
readonly prototype: Map<any, any>;
1515
}
1616
declare var Map: MapConstructor;
@@ -26,7 +26,7 @@ interface WeakMap<K, V> {
2626

2727
interface WeakMapConstructor {
2828
new (): WeakMap<any, any>;
29-
new <K, V>(): WeakMap<K, V>;
29+
new <K, V>(entries?: [K, V][]): WeakMap<K, V>;
3030
readonly prototype: WeakMap<any, any>;
3131
}
3232
declare var WeakMap: WeakMapConstructor;
@@ -35,20 +35,14 @@ interface Set<T> {
3535
add(value: T): Set<T>;
3636
clear(): void;
3737
delete(value: T): boolean;
38-
entries(): IterableIterator<[T, T]>;
3938
forEach(callbackfn: (value: T, index: T, set: Set<T>) => void, thisArg?: any): void;
4039
has(value: T): boolean;
41-
keys(): IterableIterator<T>;
4240
readonly size: number;
43-
values(): IterableIterator<T>;
44-
[Symbol.iterator]():IterableIterator<T>;
45-
readonly [Symbol.toStringTag]: "Set";
4641
}
4742

4843
interface SetConstructor {
4944
new (): Set<any>;
50-
new <T>(): Set<T>;
51-
new <T>(iterable: Iterable<T>): Set<T>;
45+
new <T>(values?: T[]): Set<T>;
5246
readonly prototype: Set<any>;
5347
}
5448
declare var Set: SetConstructor;
@@ -58,13 +52,12 @@ interface WeakSet<T> {
5852
clear(): void;
5953
delete(value: T): boolean;
6054
has(value: T): boolean;
61-
readonly [Symbol.toStringTag]: "WeakSet";
55+
6256
}
6357

6458
interface WeakSetConstructor {
6559
new (): WeakSet<any>;
66-
new <T>(): WeakSet<T>;
67-
new <T>(iterable: Iterable<T>): WeakSet<T>;
60+
new <T>(values?: T[]): WeakSet<T>;
6861
readonly prototype: WeakSet<any>;
6962
}
7063
declare var WeakSet: WeakSetConstructor;

src/lib/es2015.iterable.d.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
/// <reference path="lib.es2015.symbol.d.ts" />
22

3+
interface SymbolConstructor {
4+
/**
5+
* A method that returns the default iterator for an object. Called by the semantics of the
6+
* for-of statement.
7+
*/
8+
readonly iterator: symbol;
9+
}
10+
311
interface IteratorResult<T> {
412
done: boolean;
513
value: T;
@@ -11,11 +19,18 @@ interface Iterator<T> {
1119
throw?(e?: any): IteratorResult<T>;
1220
}
1321

14-
interface Iterable<T> { }
22+
interface Iterable<T> {
23+
[Symbol.iterator](): Iterator<T>;
24+
}
1525

16-
interface IterableIterator<T> extends Iterator<T> { }
26+
interface IterableIterator<T> extends Iterator<T> {
27+
[Symbol.iterator](): IterableIterator<T>;
28+
}
1729

1830
interface Array<T> {
31+
/** Iterator */
32+
[Symbol.iterator](): IterableIterator<T>;
33+
1934
/**
2035
* Returns an array of key, value pairs for every entry in the array
2136
*/
@@ -48,7 +63,13 @@ interface ArrayConstructor {
4863
from<T>(iterable: Iterable<T>): Array<T>;
4964
}
5065

66+
interface IArguments {
67+
/** Iterator */
68+
[Symbol.iterator](): IterableIterator<any>;
69+
}
70+
5171
interface Map<K, V> {
72+
[Symbol.iterator](): IterableIterator<[K,V]>;
5273
entries(): IterableIterator<[K, V]>;
5374
keys(): IterableIterator<K>;
5475
values(): IterableIterator<V>;
@@ -64,6 +85,23 @@ interface WeakMapConstructor {
6485
new <K, V>(iterable: Iterable<[K, V]>): WeakMap<K, V>;
6586
}
6687

88+
interface Set<T> {
89+
[Symbol.iterator](): IterableIterator<T>;
90+
entries(): IterableIterator<[T, T]>;
91+
keys(): IterableIterator<T>;
92+
values(): IterableIterator<T>;
93+
}
94+
95+
interface SetConstructor {
96+
new <T>(iterable: Iterable<T>): Set<T>;
97+
}
98+
99+
interface WeakSet<T> { }
100+
101+
interface WeakSetConstructor {
102+
new <T>(iterable: Iterable<T>): WeakSet<T>;
103+
}
104+
67105
interface Promise<T> { }
68106

69107
interface PromiseConstructor {
@@ -88,11 +126,17 @@ declare namespace Reflect {
88126
function enumerate(target: any): IterableIterator<any>;
89127
}
90128

129+
interface String {
130+
/** Iterator */
131+
[Symbol.iterator](): IterableIterator<string>;
132+
}
133+
91134
/**
92135
* A typed array of 8-bit integer values. The contents are initialized to 0. If the requested
93136
* number of bytes could not be allocated an exception is raised.
94137
*/
95138
interface Int8Array {
139+
[Symbol.iterator](): IterableIterator<number>;
96140
/**
97141
* Returns an array of key, value pairs for every entry in the array
98142
*/
@@ -124,6 +168,7 @@ interface Int8ArrayConstructor {
124168
* requested number of bytes could not be allocated an exception is raised.
125169
*/
126170
interface Uint8Array {
171+
[Symbol.iterator](): IterableIterator<number>;
127172
/**
128173
* Returns an array of key, value pairs for every entry in the array
129174
*/
@@ -155,6 +200,7 @@ interface Uint8ArrayConstructor {
155200
* If the requested number of bytes could not be allocated an exception is raised.
156201
*/
157202
interface Uint8ClampedArray {
203+
[Symbol.iterator](): IterableIterator<number>;
158204
/**
159205
* Returns an array of key, value pairs for every entry in the array
160206
*/
@@ -189,6 +235,7 @@ interface Uint8ClampedArrayConstructor {
189235
* requested number of bytes could not be allocated an exception is raised.
190236
*/
191237
interface Int16Array {
238+
[Symbol.iterator](): IterableIterator<number>;
192239
/**
193240
* Returns an array of key, value pairs for every entry in the array
194241
*/
@@ -222,6 +269,7 @@ interface Int16ArrayConstructor {
222269
* requested number of bytes could not be allocated an exception is raised.
223270
*/
224271
interface Uint16Array {
272+
[Symbol.iterator](): IterableIterator<number>;
225273
/**
226274
* Returns an array of key, value pairs for every entry in the array
227275
*/
@@ -253,6 +301,7 @@ interface Uint16ArrayConstructor {
253301
* requested number of bytes could not be allocated an exception is raised.
254302
*/
255303
interface Int32Array {
304+
[Symbol.iterator](): IterableIterator<number>;
256305
/**
257306
* Returns an array of key, value pairs for every entry in the array
258307
*/
@@ -284,6 +333,7 @@ interface Int32ArrayConstructor {
284333
* requested number of bytes could not be allocated an exception is raised.
285334
*/
286335
interface Uint32Array {
336+
[Symbol.iterator](): IterableIterator<number>;
287337
/**
288338
* Returns an array of key, value pairs for every entry in the array
289339
*/
@@ -315,6 +365,7 @@ interface Uint32ArrayConstructor {
315365
* of bytes could not be allocated an exception is raised.
316366
*/
317367
interface Float32Array {
368+
[Symbol.iterator](): IterableIterator<number>;
318369
/**
319370
* Returns an array of key, value pairs for every entry in the array
320371
*/
@@ -346,6 +397,7 @@ interface Float32ArrayConstructor {
346397
* number of bytes could not be allocated an exception is raised.
347398
*/
348399
interface Float64Array {
400+
[Symbol.iterator](): IterableIterator<number>;
349401
/**
350402
* Returns an array of key, value pairs for every entry in the array
351403
*/

src/lib/es2015.symbol.wellknown.d.ts

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ interface SymbolConstructor {
1313
*/
1414
readonly isConcatSpreadable: symbol;
1515

16-
/**
17-
* A method that returns the default iterator for an object. Called by the semantics of the
18-
* for-of statement.
19-
*/
20-
readonly iterator: symbol;
21-
2216
/**
2317
* A regular expression method that matches the regular expression against a string. Called
2418
* by the String.prototype.match method.
@@ -73,9 +67,6 @@ interface Symbol {
7367
}
7468

7569
interface Array<T> {
76-
/** Iterator */
77-
[Symbol.iterator](): IterableIterator<T>;
78-
7970
/**
8071
* Returns an object whose properties have the value 'true'
8172
* when they will be absent when used in a 'with' statement.
@@ -116,14 +107,21 @@ interface Date {
116107
}
117108

118109
interface Map<K, V> {
119-
[Symbol.iterator]():IterableIterator<[K,V]>;
120110
readonly [Symbol.toStringTag]: "Map";
121111
}
122112

123113
interface WeakMap<K, V>{
124114
readonly [Symbol.toStringTag]: "WeakMap";
125115
}
126116

117+
interface Set<T> {
118+
readonly [Symbol.toStringTag]: "Set";
119+
}
120+
121+
interface WeakSet<T> {
122+
readonly [Symbol.toStringTag]: "WeakSet";
123+
}
124+
127125
interface JSON {
128126
readonly [Symbol.toStringTag]: "JSON";
129127
}
@@ -143,21 +141,6 @@ interface GeneratorFunction extends Function {
143141
readonly [Symbol.toStringTag]: "GeneratorFunction";
144142
}
145143

146-
interface IArguments {
147-
/** Iterator */
148-
[Symbol.iterator](): IterableIterator<any>;
149-
}
150-
151-
interface Iterator<T> { }
152-
153-
interface Iterable<T> {
154-
[Symbol.iterator](): Iterator<T>;
155-
}
156-
157-
interface IterableIterator<T> extends Iterator<T> {
158-
[Symbol.iterator](): IterableIterator<T>;
159-
}
160-
161144
interface Math {
162145
readonly [Symbol.toStringTag]: "Math";
163146
}
@@ -223,9 +206,6 @@ interface RegExpConstructor {
223206
}
224207

225208
interface String {
226-
/** Iterator */
227-
[Symbol.iterator](): IterableIterator<string>;
228-
229209
/**
230210
* Matches a string an object that supports being matched against, and returns an array containing the results of that search.
231211
* @param matcher An object that supports being matched against.
@@ -279,7 +259,6 @@ interface DataView {
279259
* number of bytes could not be allocated an exception is raised.
280260
*/
281261
interface Int8Array {
282-
[Symbol.iterator](): IterableIterator<number>;
283262
readonly [Symbol.toStringTag]: "Int8Array";
284263
}
285264

@@ -288,7 +267,6 @@ interface Int8Array {
288267
* requested number of bytes could not be allocated an exception is raised.
289268
*/
290269
interface Uint8Array {
291-
[Symbol.iterator](): IterableIterator<number>;
292270
readonly [Symbol.toStringTag]: "UInt8Array";
293271
}
294272

@@ -297,7 +275,6 @@ interface Uint8Array {
297275
* If the requested number of bytes could not be allocated an exception is raised.
298276
*/
299277
interface Uint8ClampedArray {
300-
[Symbol.iterator](): IterableIterator<number>;
301278
readonly [Symbol.toStringTag]: "Uint8ClampedArray";
302279
}
303280

@@ -306,7 +283,6 @@ interface Uint8ClampedArray {
306283
* requested number of bytes could not be allocated an exception is raised.
307284
*/
308285
interface Int16Array {
309-
[Symbol.iterator](): IterableIterator<number>;
310286
readonly [Symbol.toStringTag]: "Int16Array";
311287
}
312288

@@ -315,7 +291,6 @@ interface Int16Array {
315291
* requested number of bytes could not be allocated an exception is raised.
316292
*/
317293
interface Uint16Array {
318-
[Symbol.iterator](): IterableIterator<number>;
319294
readonly [Symbol.toStringTag]: "Uint16Array";
320295
}
321296

@@ -324,7 +299,6 @@ interface Uint16Array {
324299
* requested number of bytes could not be allocated an exception is raised.
325300
*/
326301
interface Int32Array {
327-
[Symbol.iterator](): IterableIterator<number>;
328302
readonly [Symbol.toStringTag]: "Int32Array";
329303
}
330304

@@ -333,7 +307,6 @@ interface Int32Array {
333307
* requested number of bytes could not be allocated an exception is raised.
334308
*/
335309
interface Uint32Array {
336-
[Symbol.iterator](): IterableIterator<number>;
337310
readonly [Symbol.toStringTag]: "Uint32Array";
338311
}
339312

@@ -342,7 +315,6 @@ interface Uint32Array {
342315
* of bytes could not be allocated an exception is raised.
343316
*/
344317
interface Float32Array {
345-
[Symbol.iterator](): IterableIterator<number>;
346318
readonly [Symbol.toStringTag]: "Float32Array";
347319
}
348320

@@ -351,6 +323,5 @@ interface Float32Array {
351323
* number of bytes could not be allocated an exception is raised.
352324
*/
353325
interface Float64Array {
354-
[Symbol.iterator](): IterableIterator<number>;
355326
readonly [Symbol.toStringTag]: "Float64Array";
356327
}

tests/baselines/reference/argumentsObjectIterator02_ES6.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function doubleAndReturnAsArray(x: number, y: number, z: number): [number, numbe
99
let blah = arguments[Symbol.iterator];
1010
>blah : Symbol(blah, Decl(argumentsObjectIterator02_ES6.ts, 2, 7))
1111
>arguments : Symbol(arguments)
12-
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
12+
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
1313
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
14-
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
14+
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
1515

1616
let result = [];
1717
>result : Symbol(result, Decl(argumentsObjectIterator02_ES6.ts, 4, 7))

tests/baselines/reference/arrayLiterals2ES6.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ interface myArray2 extends Array<Number|String> { }
7979
>myArray2 : Symbol(myArray2, Decl(arrayLiterals2ES6.ts, 42, 43))
8080
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
8181
>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
82-
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
82+
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
8383

8484
var d0 = [1, true, ...temp, ]; // has type (string|number|boolean)[]
8585
>d0 : Symbol(d0, Decl(arrayLiterals2ES6.ts, 44, 3))

0 commit comments

Comments
 (0)