Syntax
type Union = TypeA | TypeB;
type Intersection = TypeA & TypeB;Examples
Union Types
A value that can be one of several specific types.
type ID = string | number;
function printId(id: ID) {
console.log(`ID: ${id}`);
}
printId(101); // OK
printId("abc-123"); // OK
// printId(true); // Error - boolean is not part of the unionIntersection Types
Combining multiple types into one that has all of their members.
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged; // must have BOTH name and age
const person: Person = {
name: "Alice",
age: 25
};
// A value missing either "name" or "age" would fail to type-checkBest practices
- Use union types to model a value with a small, known set of valid shapes or states, like a status field
- Use intersection types to compose smaller, reusable type fragments into a larger, complete type
- Combine unions with discriminant properties (a shared "kind" or "type" field) to enable clean type narrowing in conditional logic
- Be cautious intersecting types with conflicting property types - the result can resolve to never for that property, which is easy to miss
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.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.Type Inference
TypeScript can automatically determine a value's type from its initializer, without an explicit annotation - this is type inference. It reduces boilerplate significantly while still providing full type safety. Understanding what TypeScript infers (and when it needs help) is key to writing idiomatic, uncluttered TypeScript.
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.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.Type Inference
TypeScript can automatically determine a value's type from its initializer, without an explicit annotation - this is type inference. It reduces boilerplate significantly while still providing full type safety. Understanding what TypeScript infers (and when it needs help) is key to writing idiomatic, uncluttered TypeScript.