Objects, Interfaces and Type Aliases · Lesson 6 of 17
Type Aliases and Interface vs Type
A type alias gives a name to any type: an object shape, a union, a tuple or a function signature. For objects, type and interface are nearly interchangeable, which makes "interface vs type" one of the most common TypeScript interview questions. This lesson shows what each can do and a simple rule for choosing.
In this lesson
- Type aliases for objects, unions, tuples and functions
- Intersection types with
& - The real differences between type and interface
- A team-friendly rule of thumb
Type aliases
type ID = number | string; // a union
type Point = { x: number; y: number }; // an object shape
type Range = [start: number, end: number]; // a tuple
type Formatter = (value: number) => string; // a function type
const ids: ID[] = [101, "ORD-102"];
const origin: Point = { x: 0, y: 0 };
const marks: Range = [40, 100];
const inr: Formatter = (v) => `Rs ${v.toFixed(2)}`;
console.log(ids, origin, marks, inr(1499));
[ 101, 'ORD-102' ] { x: 0, y: 0 } [ 40, 100 ] Rs 1499.00An alias doesn't create a new type – it's just a name, so two aliases with the same structure are interchangeable.
Intersection types
type Timestamps = { createdAt: Date; updatedAt: Date };
type WithId = { id: number };
type Post = WithId & Timestamps & { title: string; tags: string[] }; // all of them combined
const post: Post = {
id: 7,
title: "Generics explained",
tags: ["typescript"],
createdAt: new Date("2026-11-01T10:00:00Z"),
updatedAt: new Date("2026-11-07T09:30:00Z"),
};
const ageDays = (post.updatedAt.getTime() - post.createdAt.getTime()) / 86_400_000;
console.log(post.id, post.title, ageDays.toFixed(1));
7 Generics explained 6.0
A & B means "has everything A has and everything B has". It's the type-alias equivalent of interface C extends A, B.
What only each can do
// Only interfaces: declaration merging – two declarations combine into one
interface Settings { theme: string }
interface Settings { fontSize: number }
const s: Settings = { theme: "dark", fontSize: 16 };
// Only type aliases: unions, tuples, primitives, mapped and conditional types
type Shape = { kind: "circle"; r: number } | { kind: "square"; side: number };
type Pair<T> = [T, T];
const shapes: Shape[] = [{ kind: "circle", r: 1 }, { kind: "square", side: 2 }];
const p: Pair<string> = ["left", "right"];
console.log(s, shapes.length, p);
{ theme: 'dark', fontSize: 16 } 2 [ 'left', 'right' ]| Feature | interface | type |
|---|---|---|
| Describe object shapes | yes | yes |
| Extend / combine | extends | & intersections |
| Unions, tuples, primitives, functions | no (functions only as call signatures) | yes |
| Mapped and conditional types | no | yes |
| Declaration merging | yes – used to extend library types | no – a duplicate name is an error |
| Error messages for objects | show the interface name | sometimes expand the full structure |
Class implements | yes | yes (for object types) |
Extending library types with merging
// Adding a property to a global type (as libraries and frameworks do)
declare global {
interface Array<T> {
lastItem(): T | undefined;
}
}
Array.prototype.lastItem = function () {
return this[this.length - 1];
};
console.log([10, 20, 30].lastItem(), [].lastItem());
export {};
30 undefined
Use merging for types, not for patching built-ins in apps
The example shows the mechanism – it's how libraries like Express add req.user to their types. Modifying built-in prototypes in your own application, though, is a bad habit: prefer a helper function.
A rule of thumb
Use interface for object shapes that others may extend (public APIs, component props, models), and type for everything else – unions, tuples, function types and computed types. Most importantly, be consistent within a codebase. Either choice is fine in an interview if you can explain the differences above.
Practice
Try it yourself
Create a type
ApiResultthat is either{ ok: true; data: string[] }or{ ok: false; error: string }, and print each case.Show solution
type ApiResult = { ok: true; data: string[] } | { ok: false; error: string }; function show(r: ApiResult): string { return r.ok ? `loaded ${r.data.length} items` : `failed: ${r.error}`; } console.log(show({ ok: true, data: ["a", "b"] })); console.log(show({ ok: false, error: "timeout" }));loaded 2 items failed: timeout
Quick check
Can an interface describe a union?
No – use a type alias.
What is declaration merging?
Two interface declarations with the same name combine into one; type aliases can't do this.
What does A & B mean?
A type with all the properties of both A and B.
Summary
- type names any type: objects, unions, tuples, functions, computed types.
- & combines types; interfaces combine with extends and can merge.
- interface for extensible object shapes, type for the rest – and stay consistent.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.