On this Page
On this Guide
- Lesson 01: What is TypeScript and Why Use It
- Lesson 02: Setting Up TypeScript in Your Project
- Lesson 03: Basic Types and Type Annotations
- Lesson 04: Functions and Type Inference
- Lesson 05: Objects, Interfaces, and Optional Properties
- Lesson 06: Arrays, Tuples, and readonly
- Lesson 07: Union, Intersection, and Literal Types
- Lesson 08: Type Aliases vs Interfaces
What is a Type Alias?
A type alias gives a name to any type:
type UserID = string;
type Status = "active" | "inactive";
type Product = {
id: number;
name: string;
};
You can use type for primitives, unions, intersections, and functions.
What is an Interface?
An interface describes the structure of an object:
interface User {
id: number;
name: string;
}
Interfaces are extendable and composable, ideal for object shapes and class contracts.
Key Differences
| Feature | type |
interface |
|---|---|---|
| Objects | ✅ | ✅ |
| Primitives, unions, etc. | ✅ (strings, unions, functions) | ❌ only objects |
| Extendable | ✅ via intersections (&) |
✅ via extends |
| Declaration merging | ❌ no | ✅ supported |
| Better for classes | ❌ not ideal | ✅ recommended |
When to Use type vs interface
Use interface when:
- Defining object structures
- Building APIs, classes, or data models
- You benefit from declaration merging
Use type when:
- Creating unions, tuples, or function signatures
- Combining multiple types with
|or& - Aliasing a primitive or non-object structure
Can You Combine Them?
Yes — you can use both together:
interface Person {
name: string;
}
type WithAge = Person & { age: number };
> You can even use a type inside an interface or vice versa.
Summary
typeis more flexible — can alias anythinginterfaceis better for structured, extendable object types- Use
interfacefor long-lived objects and APIs - Use
typefor complex unions, intersections, and primitives
Next up: Working with Enums — the right way to create named constants and semantic values.