Skip to content

Commit ac86cfe

Browse files
Renegade334marco-ippolito
authored andcommitted
typings: rationalise TypedArray types
PR-URL: #62174 Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent 0e1915e commit ac86cfe

2 files changed

Lines changed: 32 additions & 43 deletions

File tree

typings/globals.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ declare global {
8181
| BigUint64Array
8282
| BigInt64Array;
8383

84+
type TypedArrayConstructor =
85+
| typeof Uint8Array
86+
| typeof Uint8ClampedArray
87+
| typeof Uint16Array
88+
| typeof Uint32Array
89+
| typeof Int8Array
90+
| typeof Int16Array
91+
| typeof Int32Array
92+
| typeof Float16Array
93+
| typeof Float32Array
94+
| typeof Float64Array
95+
| typeof BigUint64Array
96+
| typeof BigInt64Array;
97+
8498
namespace NodeJS {
8599
interface Global {
86100
internalBinding<T extends InternalBindingKeys>(binding: T): InternalBindingMap[T]

typings/primordials.d.ts

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type UncurryGetter<O, K extends keyof O, T = O> =
1919
type UncurrySetter<O, K extends keyof O, T = O> =
2020
O[K] extends infer V ? (self: T, value: V) => void : never;
2121

22-
type TypedArrayContentType<T extends TypedArray> = T extends { [k: number]: infer V } ? V : never;
22+
type TypedArrayContentType<T extends TypedArrayConstructor> = InstanceType<T>[number];
2323

2424
/**
2525
* Primordials are a way to safely use globals without fear of global mutation
@@ -469,43 +469,30 @@ declare namespace primordials {
469469
export const SyntaxErrorPrototype: typeof SyntaxError.prototype
470470
export import TypeError = globalThis.TypeError;
471471
export const TypeErrorPrototype: typeof TypeError.prototype
472-
export function TypedArrayFrom<T extends TypedArray>(
473-
constructor: new (length: number) => T,
474-
source:
475-
| Iterable<TypedArrayContentType<T>>
476-
| ArrayLike<TypedArrayContentType<T>>,
477-
): T;
478-
export function TypedArrayFrom<T extends TypedArray, U, THIS_ARG = undefined>(
479-
constructor: new (length: number) => T,
472+
export function TypedArrayFrom<T extends TypedArrayConstructor>(
473+
constructor: T,
474+
source: Iterable<TypedArrayContentType<T>> | ArrayLike<TypedArrayContentType<T>>,
475+
): InstanceType<T>
476+
export function TypedArrayFrom<T extends TypedArrayConstructor, U, THIS_ARG = undefined>(
477+
constructor: T,
480478
source: Iterable<U> | ArrayLike<U>,
481479
mapfn: (
482480
this: THIS_ARG,
483481
value: U,
484482
index: number,
485483
) => TypedArrayContentType<T>,
486484
thisArg?: THIS_ARG,
487-
): T;
488-
export function TypedArrayOf<T extends TypedArray>(
489-
constructor: new (length: number) => T,
490-
...items: readonly TypedArrayContentType<T>[]
491-
): T;
492-
export function TypedArrayOfApply<T extends TypedArray>(
493-
constructor: new (length: number) => T,
485+
): InstanceType<T>;
486+
export function TypedArrayOf<T extends TypedArrayConstructor>(
487+
constructor: T,
488+
...items: TypedArrayContentType<T>[],
489+
): InstanceType<T>;
490+
export function TypedArrayOfApply<T extends TypedArrayConstructor>(
491+
constructor: T,
494492
items: readonly TypedArrayContentType<T>[],
495-
): T;
496-
export const TypedArray: TypedArray;
497-
export const TypedArrayPrototype:
498-
| typeof Uint8Array.prototype
499-
| typeof Int8Array.prototype
500-
| typeof Uint16Array.prototype
501-
| typeof Int16Array.prototype
502-
| typeof Uint32Array.prototype
503-
| typeof Int32Array.prototype
504-
| typeof Float32Array.prototype
505-
| typeof Float64Array.prototype
506-
| typeof BigInt64Array.prototype
507-
| typeof BigUint64Array.prototype
508-
| typeof Uint8ClampedArray.prototype;
493+
): InstanceType<T>;
494+
export const TypedArray: TypedArrayConstructor;
495+
export const TypedArrayPrototype: TypedArrayConstructor["prototype"];
509496
export const TypedArrayPrototypeGetBuffer: UncurryGetter<TypedArray, "buffer">;
510497
export const TypedArrayPrototypeGetByteLength: UncurryGetter<TypedArray, "byteLength">;
511498
export const TypedArrayPrototypeGetByteOffset: UncurryGetter<TypedArray, "byteOffset">;
@@ -516,19 +503,7 @@ declare namespace primordials {
516503
export function TypedArrayPrototypeSet<T extends TypedArray>(self: T, ...args: Parameters<T["set"]>): ReturnType<T["set"]>;
517504
export function TypedArrayPrototypeSubarray<T extends TypedArray>(self: T, ...args: Parameters<T["subarray"]>): ReturnType<T["subarray"]>;
518505
export function TypedArrayPrototypeSlice<T extends TypedArray>(self: T, ...args: Parameters<T["slice"]>): ReturnType<T["slice"]>;
519-
export function TypedArrayPrototypeGetSymbolToStringTag(self: unknown):
520-
| 'Int8Array'
521-
| 'Int16Array'
522-
| 'Int32Array'
523-
| 'Uint8Array'
524-
| 'Uint16Array'
525-
| 'Uint32Array'
526-
| 'Uint8ClampedArray'
527-
| 'BigInt64Array'
528-
| 'BigUint64Array'
529-
| 'Float32Array'
530-
| 'Float64Array'
531-
| undefined;
506+
export function TypedArrayPrototypeGetSymbolToStringTag(self: unknown): TypedArray[typeof Symbol.toStringTag] | undefined;
532507
export import URIError = globalThis.URIError;
533508
export const URIErrorPrototype: typeof URIError.prototype
534509
export import Uint16Array = globalThis.Uint16Array;

0 commit comments

Comments
 (0)