Sub topic 7
Array and tuple element types with `T[number]`
This chapter assumes you know basic TypeScript types, unions, arrays, tuples, and generics. The goal is to derive an element type from an existing array or tuple type instead of writing the same union by hand.
The meaning of `T[number]`
An indexed access type has the form T[K]. It asks TypeScript for the type of property K in type T.
Arrays have numeric indexes. Therefore, indexing an array type with number asks for the type of any element:
type Scores = number[];
type Score = Scores[number]; // number
type Names = Array<string>;
type Name = Names[number]; // stringnumber here is a type, not a specific array position. It means “any numeric index.” For a regular array, every valid numeric index has the same element type.
The same idea works with a union of element types:
type Values = (string | number)[];
type Value = Values[number]; // string | numberThis is useful when a type already describes a list and another type needs to describe one item from that list.
Tuples preserve position information
A tuple is an array type with a fixed number of elements and known types at each position:
type Point = [number, number];
type PointElement = Point[number]; // number
type Record = [number, string, boolean];
type RecordElement = Record[number]; // number | string | booleanFor a tuple, T[number] produces a union of all its element types. It does not produce a tuple and does not preserve the position.
type Entry = [id: number, label: string];
type First = Entry[0]; // number
type Second = Entry[1]; // string
type AnyEntryValue = Entry[number]; // number | stringUse a numeric literal such as 0 when you need the type at one exact position. Use number when any element is allowed.
| Type expression | Meaning | Result | Column |
|---|---|---|---|
| `string[]` | Array containing strings | `string[]` | |
| `string[][number]` | One element from that array | `string` | |
| `[number, string][0]` | First tuple element | `number` | |
| `[number, string][number]` | Any tuple element | `number \ | string` |
Deriving a union from an `as const` array
Normally, TypeScript widens array values to general types:
const statuses = ["pending", "approved", "rejected"];
type Status = typeof statuses[number]; // stringThe variable is inferred as string[], so indexing it with number gives string. TypeScript does not retain the individual string values.
as const changes this behavior. It tells TypeScript to preserve literal values and make the array readonly:
const STATUSES = ["pending", "approved", "rejected"] as const;
type Status = typeof STATUSES[number];
// "pending" | "approved" | "rejected"The expression typeof STATUSES obtains the type of the value. That type is:
readonly ["pending", "approved", "rejected"]Then [number] extracts the type of any element from that readonly tuple.
This pattern creates one source of truth. The array supplies runtime data, while the derived type supplies compile-time checking.
const ROLES = ["admin", "editor", "viewer"] as const;
type Role = typeof ROLES[number];
function describeRole(role: Role): string {
switch (role) {
case "admin":
return "Full access";
case "editor":
return "Can modify content";
case "viewer":
return "Read-only access";
}
}
const selectedRole: Role = "editor";
// const invalidRole: Role = "owner"; // Error
console.log(describeRole(selectedRole));Readonly arrays and generic helpers
An as const array is readonly, so functions that accept it should often use readonly in their parameter type:
function includesValue<T>(values: readonly T[], value: T): boolean {
return values.includes(value);
}
const colors = ["red", "green", "blue"] as const;
console.log(includesValue(colors, "red"));When you need to expose the element type of a generic array, write T[number]:
function first<T extends readonly unknown[]>(items: T): T[number] | undefined {
return items[0];
}
const firstColor = first(["red", "green", "blue"] as const);
// "red" | "green" | "blue" | undefinedThe return type describes any possible element, not necessarily the first literal. For a more precise first-element type, a separate tuple constraint is needed.
Exercise
Given const DIRECTIONS = ["north", "south", "east", "west"] as const, write a type named Direction that is the union of the four string literals.
Show answer
type Direction = typeof DIRECTIONS[number];