From df54a3d6b49071d59f67f1710cc6aa91aeda6d02 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 9 Jan 2025 16:28:50 -0500 Subject: [PATCH] Add non-generic fallback overloads for TypedArray constructors (#60934) --- src/lib/es2020.bigint.d.ts | 6 ++++-- src/lib/es5.d.ts | 9 +++++++++ .../cases/compiler/typedArrayConstructorOverloads.ts | 12 +++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lib/es2020.bigint.d.ts b/src/lib/es2020.bigint.d.ts index 05fbbf12ce146..5a258425418ed 100644 --- a/src/lib/es2020.bigint.d.ts +++ b/src/lib/es2020.bigint.d.ts @@ -373,6 +373,7 @@ interface BigInt64ArrayConstructor { new (length?: number): BigInt64Array; new (array: ArrayLike | Iterable): BigInt64Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): BigInt64Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): BigInt64Array; new (array: ArrayLike | ArrayBuffer): BigInt64Array; /** The size in bytes of each element in the array. */ @@ -650,6 +651,7 @@ interface BigUint64ArrayConstructor { new (length?: number): BigUint64Array; new (array: ArrayLike | Iterable): BigUint64Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): BigUint64Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): BigUint64Array; new (array: ArrayLike | ArrayBuffer): BigUint64Array; /** The size in bytes of each element in the array. */ @@ -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): BigUint64Array; - from(arrayLike: ArrayLike, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigUint64Array; + from(arrayLike: ArrayLike): BigUint64Array; + from(arrayLike: ArrayLike, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigUint64Array; } declare var BigUint64Array: BigUint64ArrayConstructor; diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 963f6ad24f1a7..2b0b01abe2cb6 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -2103,6 +2103,7 @@ interface Int8ArrayConstructor { new (length: number): Int8Array; new (array: ArrayLike): Int8Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Int8Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Int8Array; new (array: ArrayLike | ArrayBuffer): Int8Array; /** @@ -2384,6 +2385,7 @@ interface Uint8ArrayConstructor { new (length: number): Uint8Array; new (array: ArrayLike): Uint8Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint8Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint8Array; new (array: ArrayLike | ArrayBuffer): Uint8Array; /** @@ -2665,6 +2667,7 @@ interface Uint8ClampedArrayConstructor { new (length: number): Uint8ClampedArray; new (array: ArrayLike): Uint8ClampedArray; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint8ClampedArray; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint8ClampedArray; new (array: ArrayLike | ArrayBuffer): Uint8ClampedArray; /** @@ -2945,6 +2948,7 @@ interface Int16ArrayConstructor { new (length: number): Int16Array; new (array: ArrayLike): Int16Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Int16Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Int16Array; new (array: ArrayLike | ArrayBuffer): Int16Array; /** @@ -3226,6 +3230,7 @@ interface Uint16ArrayConstructor { new (length: number): Uint16Array; new (array: ArrayLike): Uint16Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint16Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint16Array; new (array: ArrayLike | ArrayBuffer): Uint16Array; /** @@ -3506,6 +3511,7 @@ interface Int32ArrayConstructor { new (length: number): Int32Array; new (array: ArrayLike): Int32Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Int32Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Int32Array; new (array: ArrayLike | ArrayBuffer): Int32Array; /** @@ -3786,6 +3792,7 @@ interface Uint32ArrayConstructor { new (length: number): Uint32Array; new (array: ArrayLike): Uint32Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Uint32Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Uint32Array; new (array: ArrayLike | ArrayBuffer): Uint32Array; /** @@ -4067,6 +4074,7 @@ interface Float32ArrayConstructor { new (length: number): Float32Array; new (array: ArrayLike): Float32Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Float32Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Float32Array; new (array: ArrayLike | ArrayBuffer): Float32Array; /** @@ -4348,6 +4356,7 @@ interface Float64ArrayConstructor { new (length: number): Float64Array; new (array: ArrayLike): Float64Array; new (buffer: TArrayBuffer, byteOffset?: number, length?: number): Float64Array; + new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Float64Array; new (array: ArrayLike | ArrayBuffer): Float64Array; /** diff --git a/tests/cases/compiler/typedArrayConstructorOverloads.ts b/tests/cases/compiler/typedArrayConstructorOverloads.ts index 0a656f7eaeb2f..c532545d39d1a 100644 --- a/tests/cases/compiler/typedArrayConstructorOverloads.ts +++ b/tests/cases/compiler/typedArrayConstructorOverloads.ts @@ -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); }