Syntax
type Name = TypeDefinition;Examples
Aliasing Object Shapes and Unions
The two most common uses of type aliases.
type Point = {
x: number;
y: number;
};
type Status = "pending" | "active" | "completed"; // union of literal types
function updateStatus(status: Status) {
console.log(`Status is now: ${status}`);
}
updateStatus("active");
// updateStatus("done"); // Error - "done" is not one of the allowed literalsAliasing Function Signatures and Tuples
Type aliases work for more than just objects.
type MathFn = (a: number, b: number) => number;
const add: MathFn = (a, b) => a + b;
const multiply: MathFn = (a, b) => a * b;
type Coordinate = [number, number]; // a tuple alias
const origin: Coordinate = [0, 0];Best practices
- Use type aliases for unions, tuples, and function signatures - things interface cannot directly express
- Give type aliases clear, descriptive names (Status, not T1) so error messages and autocomplete stay meaningful
- For plain object shapes, either type or interface works - pick one convention and stay consistent within a project
- Reuse a type alias across multiple functions/variables rather than repeating the same inline type definition
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook