Enhanced method signatures for typed arrays with better this typing and improved type safety. These improvements provide more precise typing for array operations.
Enhanced interface for all numeric typed arrays with improved method signatures.
interface TypedNumberArray<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> {
/**
* Determines whether all the members of an array satisfy the specified test.
* Enhanced with proper this typing.
*/
every<This = undefined>(
predicate: (
this: This,
value: number,
index: number,
array: this,
) => boolean,
thisArg?: This,
): boolean;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* Enhanced with proper this typing and return type.
*/
filter<This = undefined>(
predicate: (
this: This,
value: number,
index: number,
array: this,
) => boolean,
thisArg?: This,
): TypedNumberArray;
/**
* Returns the value of the first element in the array where predicate is true.
* Enhanced with proper this typing.
*/
find<This = undefined>(
predicate: (this: This, value: number, index: number, obj: this) => boolean,
thisArg?: This,
): number | undefined;
/**
* Returns the index of the first element in the array where predicate is true.
* Enhanced with proper this typing.
*/
findIndex<This = undefined>(
predicate: (this: This, value: number, index: number, obj: this) => boolean,
thisArg?: This,
): number;
/**
* Performs the specified action for each element in an array.
* Enhanced with proper this typing.
*/
forEach<This = undefined>(
callbackfn: (this: This, value: number, index: number, array: this) => void,
thisArg?: This,
): void;
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* Enhanced with proper this typing.
*/
map<This = undefined>(
callbackfn: (this: This, value: number, index: number, array: this) => number,
thisArg?: This,
): TypedNumberArray;
/**
* Calls the specified callback function for all the elements in an array.
* Enhanced with proper this typing.
*/
reduce<This = undefined>(
callbackfn: (
this: This,
previousValue: number,
currentValue: number,
currentIndex: number,
array: this
) => number,
initialValue?: number,
): number;
/**
* Calls the specified callback function for all the elements in an array, in descending order.
* Enhanced with proper this typing.
*/
reduceRight<This = undefined>(
callbackfn: (
this: This,
previousValue: number,
currentValue: number,
currentIndex: number,
array: this
) => number,
initialValue?: number,
): number;
/**
* Determines whether the specified callback function returns true for any element of an array.
* Enhanced with proper this typing.
*/
some<This = undefined>(
predicate: (
this: This,
value: number,
index: number,
array: this,
) => boolean,
thisArg?: This,
): boolean;
/**
* Sorts an array in place.
*/
sort(compareFn?: (a: number, b: number) => number): this;
/**
* Returns a section of an array.
*/
slice(start?: number, end?: number): TypedNumberArray;
/**
* Returns the index of the first occurrence of a value in an array.
*/
indexOf(searchElement: number, fromIndex?: number): number;
/**
* Returns the index of the last occurrence of a value in an array.
*/
lastIndexOf(searchElement: number, fromIndex?: number): number;
/**
* Gets or sets the length of the array.
*/
readonly length: number;
/**
* Returns an iterable of key, value pairs for every entry in the array.
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an iterable of keys in the array.
*/
keys(): IterableIterator<number>;
/**
* Returns an iterable of values in the array.
*/
values(): IterableIterator<number>;
[Symbol.iterator](): IterableIterator<number>;
/**
* Returns a string representation of an array.
*/
toString(): string;
/**
* Returns a string representation of an array with a specified separator.
*/
join(separator?: string): string;
}Usage Examples:
// Working with typed arrays with improved this context
const float32Array = new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5]);
// Enhanced every method with proper this typing
const allPositive = float32Array.every(function(value, index, array) {
// 'this' is properly typed based on thisArg
console.log(`Checking value ${value} at index ${index}`);
return value > 0;
});
// Enhanced filter with proper return type
const filtered = float32Array.filter(value => value > 3);
// filtered is properly typed as TypedNumberArray
// Enhanced find with proper this context
const context = { threshold: 4 };
const firstLarge = float32Array.find(function(value) {
return value > this.threshold;
}, context);
// Enhanced map operations
const doubled = float32Array.map(value => value * 2);
// doubled maintains proper typing
// Enhanced reduce operations
const sum = float32Array.reduce((acc, value) => acc + value, 0);
const product = float32Array.reduceRight((acc, value) => acc * value, 1);Enhanced definitions for specific typed array constructors.
interface Int8Array extends TypedNumberArray<ArrayBuffer> {
readonly BYTES_PER_ELEMENT: number;
readonly buffer: ArrayBuffer;
readonly byteLength: number;
readonly byteOffset: number;
/**
* Sets values in the array.
*/
set(array: ArrayLike<number>, offset?: number): void;
/**
* Returns a new array from a section of this array.
*/
subarray(begin?: number, end?: number): Int8Array;
}
interface Uint8Array extends TypedNumberArray<ArrayBuffer> {
readonly BYTES_PER_ELEMENT: number;
readonly buffer: ArrayBuffer;
readonly byteLength: number;
readonly byteOffset: number;
set(array: ArrayLike<number>, offset?: number): void;
subarray(begin?: number, end?: number): Uint8Array;
}
interface Float32Array extends TypedNumberArray<ArrayBuffer> {
readonly BYTES_PER_ELEMENT: number;
readonly buffer: ArrayBuffer;
readonly byteLength: number;
readonly byteOffset: number;
set(array: ArrayLike<number>, offset?: number): void;
subarray(begin?: number, end?: number): Float32Array;
}
interface Float64Array extends TypedNumberArray<ArrayBuffer> {
readonly BYTES_PER_ELEMENT: number;
readonly buffer: ArrayBuffer;
readonly byteLength: number;
readonly byteOffset: number;
set(array: ArrayLike<number>, offset?: number): void;
subarray(begin?: number, end?: number): Float64Array;
}Usage Examples:
// Working with specific typed array types
const uint8 = new Uint8Array([10, 20, 30, 40, 50]);
const float64 = new Float64Array([1.1, 2.2, 3.3]);
// Type-safe operations maintain specific array types
const uint8Filtered = uint8.filter(value => value > 25);
// uint8Filtered is properly typed as TypedNumberArray
const float64Doubled = float64.map(value => value * 2);
// float64Doubled is properly typed as TypedNumberArray
// Buffer operations with proper typing
console.log(`Uint8Array buffer size: ${uint8.byteLength} bytes`);
console.log(`Float64Array elements: ${float64.length}`);
// Subarray operations maintain type safety
const uint8Slice = uint8.subarray(1, 4);
// uint8Slice is properly typed as Uint8Array
const float64Slice = float64.subarray(0, 2);
// float64Slice is properly typed as Float64ArrayEnhanced ArrayBuffer and related interfaces.
interface ArrayBuffer {
/**
* Read-only byte length of the ArrayBuffer.
*/
readonly byteLength: number;
/**
* Returns a copy of this ArrayBuffer's bytes.
*/
slice(begin?: number, end?: number): ArrayBuffer;
}
interface ArrayBufferView {
/**
* The ArrayBuffer referenced by this view.
*/
readonly buffer: ArrayBuffer;
/**
* The length in bytes of this view.
*/
readonly byteLength: number;
/**
* The offset in bytes of this view from the start of its ArrayBuffer.
*/
readonly byteOffset: number;
}
interface DataView extends ArrayBufferView {
/**
* Gets the Float32 value at the specified byte offset.
*/
getFloat32(byteOffset: number, littleEndian?: boolean): number;
/**
* Gets the Float64 value at the specified byte offset.
*/
getFloat64(byteOffset: number, littleEndian?: boolean): number;
/**
* Gets the Int16 value at the specified byte offset.
*/
getInt16(byteOffset: number, littleEndian?: boolean): number;
/**
* Gets the Int32 value at the specified byte offset.
*/
getInt32(byteOffset: number, littleEndian?: boolean): number;
/**
* Gets the Uint8 value at the specified byte offset.
*/
getUint8(byteOffset: number): number;
/**
* Stores a Float32 value at the specified byte offset.
*/
setFloat32(byteOffset: number, value: number, littleEndian?: boolean): void;
/**
* Stores a Float64 value at the specified byte offset.
*/
setFloat64(byteOffset: number, value: number, littleEndian?: boolean): void;
}Usage Examples:
// Working with ArrayBuffer and DataView
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer);
// Type-safe data manipulation
view.setFloat32(0, 3.14159);
view.setInt32(4, 42);
const pi = view.getFloat32(0);
const answer = view.getInt32(4);
console.log(`Pi: ${pi}, Answer: ${answer}`);
// Buffer slicing with proper typing
const slice = buffer.slice(0, 8);
// slice is properly typed as ArrayBuffer
// Creating typed arrays from buffers
const float32View = new Float32Array(buffer, 0, 1);
const int32View = new Int32Array(buffer, 4, 1);
// Views maintain proper typing and buffer relationships
console.log(`Float32 view length: ${float32View.length}`);
console.log(`Int32 view byte offset: ${int32View.byteOffset}`);Enhanced iterator support for typed arrays.
// Typed array iterators with proper typing
interface TypedArrayIterator<T> extends Iterator<T> {
next(): IteratorResult<T>;
}
// Enhanced iteration methods
interface TypedNumberArray<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> {
/**
* Returns an iterator for array entries [index, value].
*/
entries(): IterableIterator<[number, number]>;
/**
* Returns an iterator for array indices.
*/
keys(): IterableIterator<number>;
/**
* Returns an iterator for array values.
*/
values(): IterableIterator<number>;
[Symbol.iterator](): IterableIterator<number>;
}Usage Examples:
// Enhanced iteration with proper typing
const typedArray = new Int16Array([100, 200, 300, 400]);
// Iterate over values
for (const value of typedArray) {
console.log(`Value: ${value}`); // value is properly typed as number
}
// Iterate over entries
for (const [index, value] of typedArray.entries()) {
console.log(`Index ${index}: ${value}`); // Both properly typed
}
// Iterate over keys
for (const index of typedArray.keys()) {
console.log(`Index: ${index}`); // index is properly typed as number
}
// Manual iteration
const iterator = typedArray.values();
let result = iterator.next();
while (!result.done) {
console.log(`Next value: ${result.value}`); // result.value is properly typed
result = iterator.next();
}