Skip to content

Commit 1b2a0c0

Browse files
committed
feat(lib): define TypedArray interface
Each concrete typed array type and constructor share common interfaces. Library types can be defined extending base interfaces: 1. higher code reuse 2. less duplication 3. easier maintenance (less error prone) Closes #15402 Fixes #45198
1 parent a6fb4dc commit 1b2a0c0

Some content is hidden

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

42 files changed

+1354
-5078
lines changed

src/lib/es2015.core.d.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -542,38 +542,6 @@ interface StringConstructor {
542542
raw(template: { raw: readonly string[] | ArrayLike<string>; }, ...substitutions: any[]): string;
543543
}
544544

545-
interface Int8Array {
546-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
547-
}
548-
549-
interface Uint8Array {
550-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
551-
}
552-
553-
interface Uint8ClampedArray {
554-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
555-
}
556-
557-
interface Int16Array {
558-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
559-
}
560-
561-
interface Uint16Array {
562-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
563-
}
564-
565-
interface Int32Array {
566-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
567-
}
568-
569-
interface Uint32Array {
570-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
571-
}
572-
573-
interface Float32Array {
574-
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
575-
}
576-
577-
interface Float64Array {
545+
interface TypedArray<T extends number | bigint> {
578546
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
579547
}

src/lib/es2015.iterable.d.ts

Lines changed: 25 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -220,258 +220,66 @@ interface String {
220220
[Symbol.iterator](): IterableIterator<string>;
221221
}
222222

223-
interface Int8Array {
224-
[Symbol.iterator](): IterableIterator<number>;
223+
interface TypedArray<T extends number | bigint> {
224+
[Symbol.iterator](): IterableIterator<T>;
225225
/**
226226
* Returns an array of key, value pairs for every entry in the array
227227
*/
228-
entries(): IterableIterator<[number, number]>;
228+
entries(): IterableIterator<[number, T]>;
229229
/**
230230
* Returns an list of keys in the array
231231
*/
232232
keys(): IterableIterator<number>;
233233
/**
234234
* Returns an list of values in the array
235235
*/
236-
values(): IterableIterator<number>;
236+
values(): IterableIterator<T>;
237237
}
238238

239-
interface Int8ArrayConstructor {
240-
new (elements: Iterable<number>): Int8Array;
239+
interface TypedArrayConstructor<T extends number | bigint, A extends TypedArray<T>> {
240+
new (elements: Iterable<T>): A;
241241

242242
/**
243243
* Creates an array from an array-like or iterable object.
244244
* @param arrayLike An array-like or iterable object to convert to an array.
245245
* @param mapfn A mapping function to call on every element of the array.
246246
* @param thisArg Value of 'this' used to invoke the mapfn.
247247
*/
248-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int8Array;
248+
from(arrayLike: Iterable<T>, mapfn?: (v: T, k: number) => T, thisArg?: any): A;
249249
}
250250

251-
interface Uint8Array {
252-
[Symbol.iterator](): IterableIterator<number>;
253-
/**
254-
* Returns an array of key, value pairs for every entry in the array
255-
*/
256-
entries(): IterableIterator<[number, number]>;
257-
/**
258-
* Returns an list of keys in the array
259-
*/
260-
keys(): IterableIterator<number>;
261-
/**
262-
* Returns an list of values in the array
263-
*/
264-
values(): IterableIterator<number>;
265-
}
251+
interface Int8Array extends TypedArray<number> {}
266252

267-
interface Uint8ArrayConstructor {
268-
new (elements: Iterable<number>): Uint8Array;
253+
interface Int8ArrayConstructor extends TypedArrayConstructor<number, Int8Array> {}
269254

270-
/**
271-
* Creates an array from an array-like or iterable object.
272-
* @param arrayLike An array-like or iterable object to convert to an array.
273-
* @param mapfn A mapping function to call on every element of the array.
274-
* @param thisArg Value of 'this' used to invoke the mapfn.
275-
*/
276-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array;
277-
}
255+
interface Uint8Array extends TypedArray<number> {}
278256

279-
interface Uint8ClampedArray {
280-
[Symbol.iterator](): IterableIterator<number>;
281-
/**
282-
* Returns an array of key, value pairs for every entry in the array
283-
*/
284-
entries(): IterableIterator<[number, number]>;
257+
interface Uint8ArrayConstructor extends TypedArrayConstructor<number, Uint8Array> {}
285258

286-
/**
287-
* Returns an list of keys in the array
288-
*/
289-
keys(): IterableIterator<number>;
259+
interface Uint8ClampedArray extends TypedArray<number> {}
290260

291-
/**
292-
* Returns an list of values in the array
293-
*/
294-
values(): IterableIterator<number>;
295-
}
261+
interface Uint8ClampedArrayConstructor extends TypedArrayConstructor<number, Uint8ClampedArray> {}
296262

297-
interface Uint8ClampedArrayConstructor {
298-
new (elements: Iterable<number>): Uint8ClampedArray;
263+
interface Int16Array extends TypedArray<number> {}
299264

300-
/**
301-
* Creates an array from an array-like or iterable object.
302-
* @param arrayLike An array-like or iterable object to convert to an array.
303-
* @param mapfn A mapping function to call on every element of the array.
304-
* @param thisArg Value of 'this' used to invoke the mapfn.
305-
*/
306-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8ClampedArray;
307-
}
265+
interface Int16ArrayConstructor extends TypedArrayConstructor<number, Int16Array> {}
308266

309-
interface Int16Array {
310-
[Symbol.iterator](): IterableIterator<number>;
311-
/**
312-
* Returns an array of key, value pairs for every entry in the array
313-
*/
314-
entries(): IterableIterator<[number, number]>;
267+
interface Uint16Array extends TypedArray<number> {}
315268

316-
/**
317-
* Returns an list of keys in the array
318-
*/
319-
keys(): IterableIterator<number>;
269+
interface Uint16ArrayConstructor extends TypedArrayConstructor<number, Uint16Array> {}
320270

321-
/**
322-
* Returns an list of values in the array
323-
*/
324-
values(): IterableIterator<number>;
325-
}
271+
interface Int32Array extends TypedArray<number> {}
326272

327-
interface Int16ArrayConstructor {
328-
new (elements: Iterable<number>): Int16Array;
273+
interface Int32ArrayConstructor extends TypedArrayConstructor<number, Int32Array> {}
329274

330-
/**
331-
* Creates an array from an array-like or iterable object.
332-
* @param arrayLike An array-like or iterable object to convert to an array.
333-
* @param mapfn A mapping function to call on every element of the array.
334-
* @param thisArg Value of 'this' used to invoke the mapfn.
335-
*/
336-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int16Array;
337-
}
275+
interface Uint32Array extends TypedArray<number> {}
338276

339-
interface Uint16Array {
340-
[Symbol.iterator](): IterableIterator<number>;
341-
/**
342-
* Returns an array of key, value pairs for every entry in the array
343-
*/
344-
entries(): IterableIterator<[number, number]>;
345-
/**
346-
* Returns an list of keys in the array
347-
*/
348-
keys(): IterableIterator<number>;
349-
/**
350-
* Returns an list of values in the array
351-
*/
352-
values(): IterableIterator<number>;
353-
}
277+
interface Uint32ArrayConstructor extends TypedArrayConstructor<number, Uint32Array> {}
354278

355-
interface Uint16ArrayConstructor {
356-
new (elements: Iterable<number>): Uint16Array;
279+
interface Float32Array extends TypedArray<number> {}
357280

358-
/**
359-
* Creates an array from an array-like or iterable object.
360-
* @param arrayLike An array-like or iterable object to convert to an array.
361-
* @param mapfn A mapping function to call on every element of the array.
362-
* @param thisArg Value of 'this' used to invoke the mapfn.
363-
*/
364-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint16Array;
365-
}
281+
interface Float32ArrayConstructor extends TypedArrayConstructor<number, Float32Array> {}
366282

367-
interface Int32Array {
368-
[Symbol.iterator](): IterableIterator<number>;
369-
/**
370-
* Returns an array of key, value pairs for every entry in the array
371-
*/
372-
entries(): IterableIterator<[number, number]>;
373-
/**
374-
* Returns an list of keys in the array
375-
*/
376-
keys(): IterableIterator<number>;
377-
/**
378-
* Returns an list of values in the array
379-
*/
380-
values(): IterableIterator<number>;
381-
}
382-
383-
interface Int32ArrayConstructor {
384-
new (elements: Iterable<number>): Int32Array;
385-
386-
/**
387-
* Creates an array from an array-like or iterable object.
388-
* @param arrayLike An array-like or iterable object to convert to an array.
389-
* @param mapfn A mapping function to call on every element of the array.
390-
* @param thisArg Value of 'this' used to invoke the mapfn.
391-
*/
392-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int32Array;
393-
}
394-
395-
interface Uint32Array {
396-
[Symbol.iterator](): IterableIterator<number>;
397-
/**
398-
* Returns an array of key, value pairs for every entry in the array
399-
*/
400-
entries(): IterableIterator<[number, number]>;
401-
/**
402-
* Returns an list of keys in the array
403-
*/
404-
keys(): IterableIterator<number>;
405-
/**
406-
* Returns an list of values in the array
407-
*/
408-
values(): IterableIterator<number>;
409-
}
410-
411-
interface Uint32ArrayConstructor {
412-
new (elements: Iterable<number>): Uint32Array;
413-
414-
/**
415-
* Creates an array from an array-like or iterable object.
416-
* @param arrayLike An array-like or iterable object to convert to an array.
417-
* @param mapfn A mapping function to call on every element of the array.
418-
* @param thisArg Value of 'this' used to invoke the mapfn.
419-
*/
420-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint32Array;
421-
}
422-
423-
interface Float32Array {
424-
[Symbol.iterator](): IterableIterator<number>;
425-
/**
426-
* Returns an array of key, value pairs for every entry in the array
427-
*/
428-
entries(): IterableIterator<[number, number]>;
429-
/**
430-
* Returns an list of keys in the array
431-
*/
432-
keys(): IterableIterator<number>;
433-
/**
434-
* Returns an list of values in the array
435-
*/
436-
values(): IterableIterator<number>;
437-
}
283+
interface Float64Array extends TypedArray<number> {}
438284

439-
interface Float32ArrayConstructor {
440-
new (elements: Iterable<number>): Float32Array;
441-
442-
/**
443-
* Creates an array from an array-like or iterable object.
444-
* @param arrayLike An array-like or iterable object to convert to an array.
445-
* @param mapfn A mapping function to call on every element of the array.
446-
* @param thisArg Value of 'this' used to invoke the mapfn.
447-
*/
448-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float32Array;
449-
}
450-
451-
interface Float64Array {
452-
[Symbol.iterator](): IterableIterator<number>;
453-
/**
454-
* Returns an array of key, value pairs for every entry in the array
455-
*/
456-
entries(): IterableIterator<[number, number]>;
457-
/**
458-
* Returns an list of keys in the array
459-
*/
460-
keys(): IterableIterator<number>;
461-
/**
462-
* Returns an list of values in the array
463-
*/
464-
values(): IterableIterator<number>;
465-
}
466-
467-
interface Float64ArrayConstructor {
468-
new (elements: Iterable<number>): Float64Array;
469-
470-
/**
471-
* Creates an array from an array-like or iterable object.
472-
* @param arrayLike An array-like or iterable object to convert to an array.
473-
* @param mapfn A mapping function to call on every element of the array.
474-
* @param thisArg Value of 'this' used to invoke the mapfn.
475-
*/
476-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float64Array;
477-
}
285+
interface Float64ArrayConstructor extends TypedArrayConstructor<number, Float64Array> {}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,39 +258,39 @@ interface DataView {
258258
readonly [Symbol.toStringTag]: string;
259259
}
260260

261-
interface Int8Array {
261+
interface Int8Array extends TypedArray<number> {
262262
readonly [Symbol.toStringTag]: "Int8Array";
263263
}
264264

265-
interface Uint8Array {
265+
interface Uint8Array extends TypedArray<number> {
266266
readonly [Symbol.toStringTag]: "Uint8Array";
267267
}
268268

269-
interface Uint8ClampedArray {
269+
interface Uint8ClampedArray extends TypedArray<number> {
270270
readonly [Symbol.toStringTag]: "Uint8ClampedArray";
271271
}
272272

273-
interface Int16Array {
273+
interface Int16Array extends TypedArray<number> {
274274
readonly [Symbol.toStringTag]: "Int16Array";
275275
}
276276

277-
interface Uint16Array {
277+
interface Uint16Array extends TypedArray<number> {
278278
readonly [Symbol.toStringTag]: "Uint16Array";
279279
}
280280

281-
interface Int32Array {
281+
interface Int32Array extends TypedArray<number> {
282282
readonly [Symbol.toStringTag]: "Int32Array";
283283
}
284284

285-
interface Uint32Array {
285+
interface Uint32Array extends TypedArray<number> {
286286
readonly [Symbol.toStringTag]: "Uint32Array";
287287
}
288288

289-
interface Float32Array {
289+
interface Float32Array extends TypedArray<number> {
290290
readonly [Symbol.toStringTag]: "Float32Array";
291291
}
292292

293-
interface Float64Array {
293+
interface Float64Array extends TypedArray<number> {
294294
readonly [Symbol.toStringTag]: "Float64Array";
295295
}
296296

0 commit comments

Comments
 (0)