Curriculum
Course: TypeScript
Login
Text lesson

TS Arrays

TypeScript uses a specific syntax for typing arrays.

Example

const names: string[] = [];
names.push(“Dylan”); // no error
// names.push(3); // Error: Argument of type ‘number’ is not assignable to parameter of type ‘string’.

Readonly

The readonly keyword can be used to prevent modifications to arrays.

Example

const names: readonly string[] = [“Dylan”];
names.push(“Jack”); // Error: Property ‘push’ does not exist on type ‘readonly string[]’.
// try removing the readonly modifier and see if it works?

Type Inference

TypeScript can infer the type of an array based on its values.

Example

const numbers = [123]; // inferred to type number[]
numbers.push(4); // no error
// comment line below out to see the successful assignment
numbers.push(“2”); // Error: Argument of type ‘string’ is not assignable to parameter of type ‘number’.
let head: number = numbers[0]; // no error