theory.ts

Arrays & Tuples

Typed Arrays

Arrays in TypeScript are strongly typed, meaning they must contain only elements of a specific type.

  • An array type is defined using `type[]`, such as `number[]` or `string[]`.
  • An alternative syntax uses `Array<type>`, such as `Array<number>`.
  • TypeScript enforces type consistency, preventing mixed-type arrays.
// Defining arrays
let numbers: number[] = [1, 2, 3, 4, 5];
let strings: string[] = ["apple", "banana", "cherry"];

// Alternative syntax
let values: Array<number> = [10, 20, 30];