Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.d.ts: add missing TypedArray-from-iterable signatures #58188

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
72 changes: 63 additions & 9 deletions src/lib/es2015.iterable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,19 @@ interface Int8Array {
interface Int8ArrayConstructor {
new (elements: Iterable<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.
*/
from(arrayLike: Iterable<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): Int8Array;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int8Array;
}

interface Uint8Array {
Expand All @@ -267,13 +273,19 @@ interface Uint8Array {
interface Uint8ArrayConstructor {
new (elements: Iterable<number>): Uint8Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: Iterable<number>): Uint8Array;

/**
* 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8Array;
}

interface Uint8ClampedArray {
Expand All @@ -297,13 +309,19 @@ interface Uint8ClampedArray {
interface Uint8ClampedArrayConstructor {
new (elements: Iterable<number>): Uint8ClampedArray;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: Iterable<number>): Uint8ClampedArray;

/**
* 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8ClampedArray;
}

interface Int16Array {
Expand All @@ -327,13 +345,19 @@ interface Int16Array {
interface Int16ArrayConstructor {
new (elements: Iterable<number>): Int16Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: Iterable<number>): Int16Array;

/**
* 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int16Array;
}

interface Uint16Array {
Expand All @@ -355,13 +379,19 @@ interface Uint16Array {
interface Uint16ArrayConstructor {
new (elements: Iterable<number>): Uint16Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: Iterable<number>): Uint16Array;

/**
* 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint16Array;
}

interface Int32Array {
Expand All @@ -383,13 +413,19 @@ interface Int32Array {
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.
*/
from(arrayLike: 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int32Array;
}

interface Uint32Array {
Expand All @@ -411,13 +447,19 @@ interface Uint32Array {
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.
*/
from(arrayLike: 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint32Array;
}

interface Float32Array {
Expand All @@ -439,13 +481,19 @@ interface Float32Array {
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.
*/
from(arrayLike: 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float32Array;
}

interface Float64Array {
Expand All @@ -467,11 +515,17 @@ interface Float64Array {
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.
*/
from(arrayLike: 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;
from<T>(arrayLike: Iterable<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float64Array;
}
18 changes: 14 additions & 4 deletions src/lib/es2020.bigint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,19 @@ interface BigInt64ArrayConstructor {
*/
of(...items: bigint[]): BigInt64Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<bigint> | Iterable<bigint>): BigInt64Array;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do these differ from the types in TypeArray.from? For that one, we just use Iterable.

Some guesses:

  • es2020.bigint can be used without including es2015.iterable (or whatever file makes Arrays Iterable)
  • Iterable is added here because it's in TypedArray.from, but ArrayLike stays for backward compatibility.

Copy link
Contributor Author

@Renegade334 Renegade334 Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in those cases, the ArrayLike overloads had already been defined in es5 prior to the introduction of iterables, so the ArrayLike signature and the Iterable signature exist as two separate overloads (eg. here and here). Since the bigint typed arrays are defined in es2020 for the first time, both need to be added here.

ArrayLike objects don't have to be iterable, so the method definitions need to accept both:

BigInt64Array.from(
  // ArrayLike<number> but not iterable
  {
    0: 12345678,
    1: 98765432,
    length: 2,
  },
  (n: number): bigint => BigInt(n) ** 2n,
) // BigInt64Array [ 152415765279684n, 9754610558146624n ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On that subject, though, the bigint TypedArray constructors in es2020.bigint don't look like they have a new (ArrayLike<bigint>) signature, which probably also needs addressing.


/**
* 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: ArrayLike<bigint>): BigInt64Array;
from<U>(arrayLike: ArrayLike<U>, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigInt64Array;
from<T>(arrayLike: ArrayLike<T> | Iterable<T>, mapfn: (v: T, k: number) => bigint, thisArg?: any): BigInt64Array;
}

declare var BigInt64Array: BigInt64ArrayConstructor;
Expand Down Expand Up @@ -656,14 +661,19 @@ interface BigUint64ArrayConstructor {
*/
of(...items: bigint[]): BigUint64Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<bigint> | Iterable<bigint>): BigUint64Array;

/**
* 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: ArrayLike<bigint>): BigUint64Array;
from<U>(arrayLike: ArrayLike<U>, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigUint64Array;
from<T>(arrayLike: ArrayLike<T> | Iterable<T>, mapfn: (v: T, k: number) => bigint, thisArg?: any): BigUint64Array;
}

declare var BigUint64Array: BigUint64ArrayConstructor;
Expand Down
84 changes: 84 additions & 0 deletions tests/baselines/reference/typedArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,51 @@ function CreateTypedArraysFromThisObj2<T>(obj:ArrayLike<T>, mapFn: (n:T, v:numbe
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);

return typedArrays;
}

function CreateTypedArraysFromIterable(obj:Iterable<number>) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj);
typedArrays[1] = Uint8Array.from(obj);
typedArrays[2] = Int16Array.from(obj);
typedArrays[3] = Uint16Array.from(obj);
typedArrays[4] = Int32Array.from(obj);
typedArrays[5] = Uint32Array.from(obj);
typedArrays[6] = Float32Array.from(obj);
typedArrays[7] = Float64Array.from(obj);
typedArrays[8] = Uint8ClampedArray.from(obj);

return typedArrays;
}

function CreateTypedArraysFromMappedIterable(obj:Iterable<number>, mapFn: (n:number, v:number)=> number) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
typedArrays[1] = Uint8Array.from(obj, mapFn);
typedArrays[2] = Int16Array.from(obj, mapFn);
typedArrays[3] = Uint16Array.from(obj, mapFn);
typedArrays[4] = Int32Array.from(obj, mapFn);
typedArrays[5] = Uint32Array.from(obj, mapFn);
typedArrays[6] = Float32Array.from(obj, mapFn);
typedArrays[7] = Float64Array.from(obj, mapFn);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);

return typedArrays;
}

function CreateTypedArraysFromMappedIterable2<T>(obj:Iterable<T>, mapFn: (n:T, v:number)=> number) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
typedArrays[1] = Uint8Array.from(obj, mapFn);
typedArrays[2] = Int16Array.from(obj, mapFn);
typedArrays[3] = Uint16Array.from(obj, mapFn);
typedArrays[4] = Int32Array.from(obj, mapFn);
typedArrays[5] = Uint32Array.from(obj, mapFn);
typedArrays[6] = Float32Array.from(obj, mapFn);
typedArrays[7] = Float64Array.from(obj, mapFn);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);

return typedArrays;
}

Expand Down Expand Up @@ -310,3 +355,42 @@ function CreateTypedArraysFromThisObj2(obj, mapFn, thisArg) {
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
return typedArrays;
}
function CreateTypedArraysFromIterable(obj) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj);
typedArrays[1] = Uint8Array.from(obj);
typedArrays[2] = Int16Array.from(obj);
typedArrays[3] = Uint16Array.from(obj);
typedArrays[4] = Int32Array.from(obj);
typedArrays[5] = Uint32Array.from(obj);
typedArrays[6] = Float32Array.from(obj);
typedArrays[7] = Float64Array.from(obj);
typedArrays[8] = Uint8ClampedArray.from(obj);
return typedArrays;
}
function CreateTypedArraysFromMappedIterable(obj, mapFn) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
typedArrays[1] = Uint8Array.from(obj, mapFn);
typedArrays[2] = Int16Array.from(obj, mapFn);
typedArrays[3] = Uint16Array.from(obj, mapFn);
typedArrays[4] = Int32Array.from(obj, mapFn);
typedArrays[5] = Uint32Array.from(obj, mapFn);
typedArrays[6] = Float32Array.from(obj, mapFn);
typedArrays[7] = Float64Array.from(obj, mapFn);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);
return typedArrays;
}
function CreateTypedArraysFromMappedIterable2(obj, mapFn) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
typedArrays[1] = Uint8Array.from(obj, mapFn);
typedArrays[2] = Int16Array.from(obj, mapFn);
typedArrays[3] = Uint16Array.from(obj, mapFn);
typedArrays[4] = Int32Array.from(obj, mapFn);
typedArrays[5] = Uint32Array.from(obj, mapFn);
typedArrays[6] = Float32Array.from(obj, mapFn);
typedArrays[7] = Float64Array.from(obj, mapFn);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);
return typedArrays;
}
Loading