Arrays, Tuples & Objects
Beginner · 18 min read · ▶ live playground · ✦ checkpoint
Arrays, tuples, and object types are how TypeScript describes containers instead of single primitive values. Use string[] or Array<string> for same-kind lists, object type literals for named fields, and tuples when each position has its own promise.
A container type is a type for a value that holds other values. Think of it as a label on the container: arrays label every item by one rule, objects label named fields, and tuples label exact seats.
Arrays: one rule for every item
An array type is a type for an ordered list where every item follows the same rule. string[] means "an array of strings." number[] means "an array of numbers."
There is a second spelling: Array<string>. It means the same thing as string[]. Angle brackets are the < > characters around the item type in that spelling; lesson 7.1 teaches the deeper pattern.
Inference already knew launchNames was string[]. The launchYears annotation uses the other spelling so you can read both. The boundary rule still holds: annotate the function inputs and return, then let the middle infer itself.
Objects: named fields
An object type literal is a type written with braces and property names to describe an object's shape. A property is a named value on an object, like name or karma.
For reusable shapes, give the object type a name:
A type alias is a name you give to a type so you can reuse it. Here, UserProfile is the reusable promise. The object must have name, plan, and karma, with the exact types promised.
Optional properties
An optional property is a property marked with ?, meaning the object may omit it. nickname?: string says the property may be missing, and reading it gives string | undefined; when the value is not undefined, it must be a string.
When you read an optional property, TypeScript treats the value as string | undefined. A union type is an either-or type written with |; lesson 4.1 teaches that model properly. For now, the practical rule is clear: handle the missing case before treating the value like a definite string.
Nested shapes point to nested mistakes
A nested shape is an object type that contains another object or array type inside it. TypeScript does not stop at the top level. It follows the shape down to the exact wrong value.
type LaunchReport = {
owner: {
name: string;
plan: string;
};
metrics: {
launches: number;
tags: string[];
};
};
const report: LaunchReport = {
owner: {
name: "Mina",
plan: "pro",
},
metrics: {
launches: "three",
tags: ["alpha", 2026],
},
};→ playground.ts:18:5 - error TS2322: Type 'string' is not assignable to type 'number'.
→ playground.ts:19:21 - error TS2322: Type 'number' is not assignable to type 'string'.
The first diagnostic points at launches. The second points inside tags. That is the checker reading the container label all the way down.
Tuples: position is part of the promise
A tuple is an array type that checks a fixed set of positions. [string, number] means position 0 must be a string and position 1 must be a number.
Arrays are for "zero or more values that all follow one item rule." Tuples are for "these positions have these meanings." A badge, map coordinate, or small return pair can be a tuple because position carries meaning. One sharp edge: normal tuples are still mutable arrays at runtime, so array methods like push exist. Treat the guarantee here as assignment and index-position checking, not a locked runtime length.
Here is that trap as code:
const looseBadge: (string | number)[] = ["Mina", 42];
const strictBadge: [string, number] = looseBadge;→ playground.ts:2:7 - error TS2322: Type '(string | number)[]' is not assignable to type '[string, number]'.
Target requires 2 element(s) but source may have fewer.
The tuple promises seat 0 is string and seat 1 is number when you assign or read those positions. The mixed array promises none of that. It might be empty. It might have five items. It might start with a number.
You now have the everyday shapes: primitives, arrays, object type literals, optional properties, and tuples. Next lesson names the special types that sit outside this normal flow: any, unknown, never, and void.
Checkpoint
Answer all three to mark this lesson complete