HomeToolsAbout a20k

Array

Array Declaration

// literal let fruits: string[] = ['Apple', 'Orange', 'Banana']; // generics let fruits: Array<string> = ['Apple', 'Orange', 'Banana'];

[string] vs string[]

Differences

  • [string] is a tuple
  • string[] is an array of strings

What is Tuple

Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same

// Declare a tuple type let x: [string, number]; // Initialize it x = ["hello", 10]; // OK // Initialize it incorrectly x = [10, "hello"]; // Error // Error: Tuple type '[string, number]' of length '2' has no element at index '3'. x[3] = "world";
© VincentVanKoh