Syntax
let arr: Type[];
let tuple: [Type1, Type2];Examples
Typed Arrays
Restricting an array to hold only elements of a specific type.
let numbers: number[] = [1, 2, 3];
let names: Array<string> = ["Alice", "Bob"]; // equivalent generic syntax
// numbers.push("four"); // Error - "four" is not a number
let mixed: (string | number)[] = ["id", 101, "name", "Alice"]; // union arrayTuples for Fixed-Shape Data
A fixed-length array where each position has its own specific type.
let point: [number, number] = [10, 20];
let nameAge: [string, number] = ["Alice", 25];
// nameAge = [25, "Alice"]; // Error - types are in the wrong positions
// A common pattern: React-style useState-esque tuple return
function useToggle(): [boolean, () => void] {
let state = false;
const toggle = () => { state = !state; };
return [state, toggle];
}Best practices
- Use Type[] for collections where every element serves the same purpose and the length is not fixed
- Use tuples when a fixed number of values with distinct meanings need to travel together, like a coordinate pair or a [value, setter] pair
- Add readonly before a tuple or array type (readonly [number, number]) when its contents should never be mutated after creation
- Prefer a named object over a tuple with more than 2-3 elements - positional meaning becomes hard to remember beyond that
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.