Skip to content

Commit

Permalink
feat(lib): define TypedArray interface
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mfulton26 committed Jul 9, 2024
1 parent a6fb4dc commit 1b2a0c0
Show file tree
Hide file tree
Showing 42 changed files with 1,354 additions and 5,078 deletions.
34 changes: 1 addition & 33 deletions src/lib/es2015.core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,38 +542,6 @@ interface StringConstructor {
raw(template: { raw: readonly string[] | ArrayLike<string>; }, ...substitutions: any[]): string;
}

interface Int8Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Uint8Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Uint8ClampedArray {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Int16Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Uint16Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Int32Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Uint32Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Float32Array {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}

interface Float64Array {
interface TypedArray<T extends number | bigint> {
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string;
}
242 changes: 25 additions & 217 deletions src/lib/es2015.iterable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,258 +220,66 @@ interface String {
[Symbol.iterator](): IterableIterator<string>;
}

interface Int8Array {
[Symbol.iterator](): IterableIterator<number>;
interface TypedArray<T extends number | bigint> {
[Symbol.iterator](): IterableIterator<T>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
entries(): IterableIterator<[number, T]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
values(): IterableIterator<T>;
}

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

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

interface Uint8Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}
interface Int8Array extends TypedArray<number> {}

interface Uint8ArrayConstructor {
new (elements: Iterable<number>): Uint8Array;
interface Int8ArrayConstructor extends TypedArrayConstructor<number, Int8Array> {}

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

interface Uint8ClampedArray {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
interface Uint8ArrayConstructor extends TypedArrayConstructor<number, Uint8Array> {}

/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
interface Uint8ClampedArray extends TypedArray<number> {}

/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}
interface Uint8ClampedArrayConstructor extends TypedArrayConstructor<number, Uint8ClampedArray> {}

interface Uint8ClampedArrayConstructor {
new (elements: Iterable<number>): Uint8ClampedArray;
interface Int16Array extends TypedArray<number> {}

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

interface Int16Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
interface Uint16Array extends TypedArray<number> {}

/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
interface Uint16ArrayConstructor extends TypedArrayConstructor<number, Uint16Array> {}

/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}
interface Int32Array extends TypedArray<number> {}

interface Int16ArrayConstructor {
new (elements: Iterable<number>): Int16Array;
interface Int32ArrayConstructor extends TypedArrayConstructor<number, Int32Array> {}

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

interface Uint16Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}
interface Uint32ArrayConstructor extends TypedArrayConstructor<number, Uint32Array> {}

interface Uint16ArrayConstructor {
new (elements: Iterable<number>): Uint16Array;
interface Float32Array extends TypedArray<number> {}

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

interface Int32Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}

interface Int32ArrayConstructor {
new (elements: Iterable<number>): Int32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int32Array;
}

interface Uint32Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}

interface Uint32ArrayConstructor {
new (elements: Iterable<number>): Uint32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint32Array;
}

interface Float32Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}
interface Float64Array extends TypedArray<number> {}

interface Float32ArrayConstructor {
new (elements: Iterable<number>): Float32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float32Array;
}

interface Float64Array {
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<number>;
}

interface Float64ArrayConstructor {
new (elements: Iterable<number>): Float64Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float64Array;
}
interface Float64ArrayConstructor extends TypedArrayConstructor<number, Float64Array> {}
18 changes: 9 additions & 9 deletions src/lib/es2015.symbol.wellknown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,39 +258,39 @@ interface DataView {
readonly [Symbol.toStringTag]: string;
}

interface Int8Array {
interface Int8Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Int8Array";
}

interface Uint8Array {
interface Uint8Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Uint8Array";
}

interface Uint8ClampedArray {
interface Uint8ClampedArray extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Uint8ClampedArray";
}

interface Int16Array {
interface Int16Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Int16Array";
}

interface Uint16Array {
interface Uint16Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Uint16Array";
}

interface Int32Array {
interface Int32Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Int32Array";
}

interface Uint32Array {
interface Uint32Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Uint32Array";
}

interface Float32Array {
interface Float32Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Float32Array";
}

interface Float64Array {
interface Float64Array extends TypedArray<number> {
readonly [Symbol.toStringTag]: "Float64Array";
}

Expand Down
Loading

0 comments on commit 1b2a0c0

Please sign in to comment.