TypeScript provides two primary ways to define custom types: type aliases and interfaces. Both are used to describe the shape of an object or other types, but they have distinct features and use cases.
A type alias is a way to give a type a name. It can represent strings,objects, arrays, and more.
type AliasName = TypeDefinition;type StringAlias = string;
type Point = { x: number; y: number };
type UnionType = string | number;- Can represent any type, including primitives, unions, intersections, and tuples.
- Cannot be reopened or extended after creation.
An interface is like aliases, but only apply to object types.
interface InterfaceName {
property: Type;
}interface Point {
x: number;
y: number;
}
interface Drawable extends Point {
draw(): void;
}- Can only describe object shapes.
- Supports extension through inheritance (
extendskeyword). - Can be reopened and augmented across different declarations.