bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/TypeScript/TypeScript Core
TypeScript•TypeScript Core

TypeScript Arrays

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind TypeScript Arrays?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ names: string[] = [];
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The readonly keyword can prevent arrays from being changed.
Read more about arrays in our JavaScript Array chapter .
TypeScript has a specific syntax for typing arrays.

TypeScript has a specific syntax for typing arrays.

Read more about arrays in our JavaScript Array chapter .

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 prevent arrays from being changed.

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 if it has values.

Example

const numbers = [1, 2, 3]; // 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

Previous

TypeScript Special Types

Next

TypeScript Tuples