Arrays and tuples

string[] syntax

An array type names the item type followed by square brackets. Every item must match, so a stray number among strings stops the compile at once:

const tags: string[] = ["ts", "types"]; console.log(tags.length);

2

The tags array holds exactly two strings, so length reports 2. Pushing a number into tags would fail before the program ever runs.

Array<string> form

The same array can be written in generic form with the item type inside angle brackets. Both spellings mean the same thing, so teams pick one and stay with it:

const scores: Array<number> = [10, 20]; console.log(scores[0]);

10

Index zero holds 10, which the program prints. The short string[] spelling dominates simple cases, while the generic spelling shines inside complex nested types.

Fixed tuples

A tuple fixes both the length and the type of each slot. It fits pairs like coordinates, where position zero and position one play different roles:

const point: [number, number] = [10, 20]; console.log(point[0] + point[1]);

30

Swapping in a third item or a string would break the compile. Use tuples for short fixed records and plain arrays for collections of unknown length.

Readonly arrays

The readonly marker freezes an array at compile time. Reading stays free, while push and direct index writes are rejected before they can corrupt shared data:

const nums: readonly number[] = [1, 2, 3]; console.log(nums.length);

3

Mark arrays readonly when a function only needs to read them. Callers keep full control of their data, and accidents turn into compile errors instead of silent bugs.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the type string[] say about an array?

    Answer

    Every item is a string

    The item type comes first and the brackets after it say that there may be any number of those items.

  2. How do string[] and Array<string> differ?

    Answer

    They mean the same thing

    The two spellings are the same array type, so a team picks one and stays with it.

  3. What does a tuple such as [number, number] fix?

    Answer

    The length and the type of every slot

    Each position has its own type and the length is fixed, which fits a pair such as coordinates.

  4. What does this program print? const tags: string[] = ["ts", "types"]; console.log(tags.length);

    Answer

    2

    The array holds two strings, and length counts them.

  5. What does this program print? const scores: Array<number> = [10, 20]; console.log(scores[1]);

    Answer

    20

    Indexes start at zero, so index 1 is the second item.

  6. What does this program print? const point: [number, number] = [10, 20]; console.log(point[0] + point[1]);

    Answer

    30

    Both slots are numbers, so + adds them.

  7. What does the compiler say about this program? const tags: string[] = ["ts", 5]; console.log(tags.length);

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    The array type allows strings only, so the number in the second slot stops the build.

  8. What does the compiler say about this program? const point: [number, number] = [10, 20, 30]; console.log(point[0]);

    Answer

    error TS2322: Type '[number, number, number]' is not assignable to type '[number, number]'.

    The tuple type fixes the length at two, so a third item is a type error.

  9. What does the compiler say about this program? const nums: readonly number[] = [1, 2, 3]; nums.push(4); console.log(nums.length);

    Answer

    error TS2339: Property 'push' does not exist on type 'readonly number[]'.

    A readonly array may be read but not changed, so the method that adds an item is not available.

  10. Why mark a parameter as readonly number[]?

    Answer

    It promises the function does not change the caller's array

    The caller keeps control of the data, and an accidental write becomes a compile error inside the function.

Article author: Arthur Isaev