Skip to content

Commit

Permalink
Add non-generic fallback overloads for TypedArray constructors (#60934)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton authored Jan 9, 2025
1 parent e973805 commit df54a3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/lib/es2020.bigint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ interface BigInt64ArrayConstructor {
new (length?: number): BigInt64Array<ArrayBuffer>;
new (array: ArrayLike<bigint> | Iterable<bigint>): BigInt64Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): BigInt64Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): BigInt64Array<ArrayBuffer>;
new (array: ArrayLike<bigint> | ArrayBuffer): BigInt64Array<ArrayBuffer>;

/** The size in bytes of each element in the array. */
Expand Down Expand Up @@ -650,6 +651,7 @@ interface BigUint64ArrayConstructor {
new (length?: number): BigUint64Array<ArrayBuffer>;
new (array: ArrayLike<bigint> | Iterable<bigint>): BigUint64Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): BigUint64Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): BigUint64Array<ArrayBuffer>;
new (array: ArrayLike<bigint> | ArrayBuffer): BigUint64Array<ArrayBuffer>;

/** The size in bytes of each element in the array. */
Expand All @@ -667,8 +669,8 @@ interface BigUint64ArrayConstructor {
* @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(arrayLike: ArrayLike<bigint>): BigUint64Array<ArrayBuffer>;
from<U>(arrayLike: ArrayLike<U>, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigUint64Array<ArrayBuffer>;
}
declare var BigUint64Array: BigUint64ArrayConstructor;

Expand Down
9 changes: 9 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,7 @@ interface Int8ArrayConstructor {
new (length: number): Int8Array<ArrayBuffer>;
new (array: ArrayLike<number>): Int8Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Int8Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Int8Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Int8Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -2384,6 +2385,7 @@ interface Uint8ArrayConstructor {
new (length: number): Uint8Array<ArrayBuffer>;
new (array: ArrayLike<number>): Uint8Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint8Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint8Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Uint8Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -2665,6 +2667,7 @@ interface Uint8ClampedArrayConstructor {
new (length: number): Uint8ClampedArray<ArrayBuffer>;
new (array: ArrayLike<number>): Uint8ClampedArray<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint8ClampedArray<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint8ClampedArray<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Uint8ClampedArray<ArrayBuffer>;

/**
Expand Down Expand Up @@ -2945,6 +2948,7 @@ interface Int16ArrayConstructor {
new (length: number): Int16Array<ArrayBuffer>;
new (array: ArrayLike<number>): Int16Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Int16Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Int16Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Int16Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -3226,6 +3230,7 @@ interface Uint16ArrayConstructor {
new (length: number): Uint16Array<ArrayBuffer>;
new (array: ArrayLike<number>): Uint16Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint16Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint16Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Uint16Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -3506,6 +3511,7 @@ interface Int32ArrayConstructor {
new (length: number): Int32Array<ArrayBuffer>;
new (array: ArrayLike<number>): Int32Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Int32Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Int32Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Int32Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -3786,6 +3792,7 @@ interface Uint32ArrayConstructor {
new (length: number): Uint32Array<ArrayBuffer>;
new (array: ArrayLike<number>): Uint32Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint32Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint32Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Uint32Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -4067,6 +4074,7 @@ interface Float32ArrayConstructor {
new (length: number): Float32Array<ArrayBuffer>;
new (array: ArrayLike<number>): Float32Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Float32Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Float32Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Float32Array<ArrayBuffer>;

/**
Expand Down Expand Up @@ -4348,6 +4356,7 @@ interface Float64ArrayConstructor {
new (length: number): Float64Array<ArrayBuffer>;
new (array: ArrayLike<number>): Float64Array<ArrayBuffer>;
new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): Float64Array<TArrayBuffer>;
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Float64Array<ArrayBuffer>;
new (array: ArrayLike<number> | ArrayBuffer): Float64Array<ArrayBuffer>;

/**
Expand Down
12 changes: 11 additions & 1 deletion tests/cases/compiler/typedArrayConstructorOverloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
// https://github.com/microsoft/TypeScript/issues/60367

type TypedArrayConstructor =
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Uint8ClampedArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor
| BigInt64ArrayConstructor
| BigUint64ArrayConstructor

export function makeTypedArray(buffer: ArrayBuffer, ctr: TypedArrayConstructor) {
return new ctr(buffer);
new ctr(buffer);
new ctr(buffer, 0, 0);
}

0 comments on commit df54a3d

Please sign in to comment.