0
0
Typescriptprogramming~5 mins

Why typed arrays matter in Typescript

Choose your learning style9 modes available
Introduction

Typed arrays help your program handle numbers and data faster and use less memory. They make sure your data is the right type, so your program works better and safer.

When you need to work with lots of numbers quickly, like in games or animations.
When you want to send or receive data from devices or files that use fixed formats.
When you want to save memory by using the smallest data type needed.
When you want to avoid mistakes by making sure data is always the right type.
When you work with WebGL or other graphics APIs that require typed data.
Syntax
Typescript
class TypedArrayExample {
  private data: Uint8Array;

  constructor(size: number) {
    this.data = new Uint8Array(size);
  }

  setValue(index: number, value: number): void {
    this.data[index] = value;
  }

  getValue(index: number): number {
    return this.data[index];
  }

  printAll(): void {
    console.log(this.data);
  }
}

Typed arrays are classes like Uint8Array, Float32Array, etc., each for a specific type of number.

They store data in a fixed size and type, unlike normal arrays that can hold any type.

Examples
This creates a typed array of 3 bytes and sets values. It prints the array showing the numbers.
Typescript
const bytes = new Uint8Array(3);
bytes[0] = 255;
bytes[1] = 128;
bytes[2] = 64;
console.log(bytes);
This creates a typed array for 32-bit floating numbers and prints them.
Typescript
const floats = new Float32Array(2);
floats[0] = 1.5;
floats[1] = 2.5;
console.log(floats);
This shows an empty typed array with zero length.
Typescript
const emptyArray = new Uint16Array(0);
console.log(emptyArray);
This shows a typed array with one element, demonstrating the smallest signed 8-bit integer.
Typescript
const singleValue = new Int8Array(1);
singleValue[0] = -128;
console.log(singleValue);
Sample Program

This program creates a typed array of 5 bytes, shows it empty, sets some values, then shows the updated array and a single value.

Typescript
class TypedArrayExample {
  private data: Uint8Array;

  constructor(size: number) {
    this.data = new Uint8Array(size);
  }

  setValue(index: number, value: number): void {
    this.data[index] = value;
  }

  getValue(index: number): number {
    return this.data[index];
  }

  printAll(): void {
    console.log(this.data);
  }
}

const example = new TypedArrayExample(5);
console.log('Before setting values:');
example.printAll();

example.setValue(0, 10);
example.setValue(1, 20);
example.setValue(4, 255);

console.log('After setting values:');
example.printAll();

console.log('Value at index 1:', example.getValue(1));
OutputSuccess
Important Notes

Typed arrays have fixed size and type, so you cannot add or remove elements like normal arrays.

They use less memory and are faster for number-heavy tasks.

Common mistake: Trying to store values outside the allowed range (e.g., 300 in Uint8Array) will wrap or truncate the value.

Use typed arrays when you need performance and memory control, otherwise normal arrays are easier to use.

Summary

Typed arrays store numbers in fixed size and type for better speed and memory use.

They are useful for graphics, device data, and large numeric data.

Typed arrays prevent type errors and help your program run faster.