Skip to main content
Okay — let’s look at arrays and objects in TypeScript. Elmer has multiple ducks and needs a way to store them together. This guide shows how to model lists with arrays and how to represent richer item data with objects and type annotations in TypeScript. You’ll learn basic syntax, how to combine arrays and objects, and best practices for reading and mutating these structures.

Quick overview

  • Arrays: ordered collections of values (e.g., a list of duck names).
  • Objects: group related values into a single item (e.g., name, age, type, color).
  • Combining both: arrays of objects let you model collections of richly-described items (e.g., a flock of ducks).

Arrays

Declare an array of strings in TypeScript using either bracket syntax or the generic Array<T> form. Both are equivalent; pick the style your team prefers.
or
Both examples enforce that every element in the array is a string. Table — common array syntaxes Useful array operations (examples)

Objects

When you need to store multiple properties about a single duck (for example: name, age, type, and color), use an object. Object literal (untyped):
Typed object using an interface for stronger guarantees:
You can also use a type alias if you prefer:

Accessing properties

Use dot notation for the common case:
Console output:
Bracket notation is useful when the property name is dynamic:
In the object { name: 'Daffy', age: 3, type: 'Mallard', color: 'Black' }:
  • Keys (properties): name, age, type, color
  • Values: 'Daffy', 3, 'Mallard', 'Black'

Putting arrays and objects together

Most real-world data models use arrays of objects. For example, a typed collection of Duck objects:
Access items and their properties:
Common operations on an array of objects:
  • Find an item: flock.find(d => d.name === 'Howard')
  • Filter: flock.filter(d => d.age > 2)
  • Map to a new shape: flock.map(d => d.name)

Note about const and mutation

Declaring a variable with const prevents reassignment of the identifier but does not make the object or array immutable. You can still modify properties or change the array contents:
If you need immutability at the type level, prefer readonly properties and readonly arrays (e.g., readonly Duck[] or ReadonlyArray<Duck>).

Summary

  • Use T[] or Array<T> to type arrays.
  • Use object literals for single items and interface or type to enforce a shape for objects.
  • Combine arrays and objects to model lists of rich items (e.g., Duck[]).
  • const protects the binding, not the contents — use readonly for immutability when appropriate.

Watch Video