0Pricing
TypeScript Academy · Lesson

Type Aliases and Interfaces

Compare and use type aliases and interfaces.

Type Aliases and Interfaces is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Type Aliases and Interfaces

Welcome to the next lesson! In this lesson, you’ll learn about type aliases and interfaces in TypeScript, including their differences, similarities, and when to use each. Let’s get started!

Type Aliases and Interfaces — illustration 1

2

What Are Type Aliases?

A type alias allows you to define a custom name for a type. It can represent primitive types, objects, or even function signatures.

Example:

Task: Create a type alias for an object representing a book and use it to define a variable.

type Point = {
  x: number;
  y: number;
};

let point: Point = { x: 10, y: 20 };
console.log(point); 
// Output: { x: 10, y: 20 }

3

What Are Interfaces?

An interface defines the structure of an object, including its properties and methods. It’s often used to enforce consistency and type safety for objects.

Example:

Task: Create an interface for a user profile and use it to define a variable.

interface Point {
  x: number;
  y: number;
}

let point: Point = { x: 5, y: 15 };
console.log(point); 
// Output: { x: 5, y: 15 }

4

Type Aliases vs Interfaces: Similarities

Type aliases and interfaces share some features:

  • Both can describe the shape of an object.
  • Both can be used to define function types.
  • Both support extending other types.

Example: A function type:

Task: Define both a type alias and an interface for a function and use them.

type Add = (a: number, b: number) => number;
interface Subtract {
  (a: number, b: number): number;
}

let add: Add = (x, y) => x + y;
let subtract: Subtract = (x, y) => x - y;

console.log(add(5, 3)); 
// Output: 8
console.log(subtract(5, 3)); 
// Output: 2

5

Type Aliases vs Interfaces: Differences

Here are the key differences:

  • Interfaces: Can be merged (declaration merging).
  • Type Aliases: Cannot be merged.
  • Type Aliases: Can define union and intersection types.
  • Interfaces: Are primarily designed for object shapes.

Example: Union types with type aliases:

Task: Use a type alias to define a union type and assign different values to a variable.

type StringOrNumber = string | number;

let value: StringOrNumber = "Hello";
value = 42;
// value = true; 
// Error: Type 'true' is not assignable to type 'StringOrNumber'.

6

Extending Types and Interfaces

Both type aliases and interfaces can extend other types:

  • Interfaces: Use the extends keyword.
  • Type Aliases: Use intersections (&).

Example:

Task: Extend a type alias and an interface to create new types.

interface Animal {
  name: string;
}

interface Dog extends Animal {
  breed: string;
}

type Bird = Animal & { canFly: boolean };

let myDog: Dog = { name: "Buddy", breed: "Golden Retriever" };
let myBird: Bird = { name: "Parrot", canFly: true };

console.log(myDog, myBird);

7

Declaration Merging

Interfaces support declaration merging, where multiple interface declarations with the same name are merged into one. Type aliases do not support this feature.

Example:

Task: Create two interface declarations with the same name and test declaration merging.

interface Person {
  name: string;
}

interface Person {
  age: number;
}

let person: Person = { name: "Alice", age: 30 };
console.log(person); 
// Output: { name: "Alice", age: 30 }

8

When to Use Each

Here are some guidelines:

  • Use interfaces for defining object shapes and when declaration merging is needed.
  • Use type aliases for unions, intersections, or when defining function types.

Tip: Use the one that best fits your use case for clarity and consistency!

9

10

Great Job!

Congratulations! You’ve learned the similarities and differences between type aliases and interfaces in TypeScript, as well as when to use each. In the next lesson, we’ll explore advanced TypeScript features like conditional types. Let’s keep coding!

Type Aliases and Interfaces — illustration 10

Frequently asked questions

Is the “Type Aliases and Interfaces” lesson free?

Yes — the full text of “Type Aliases and Interfaces” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Type Aliases and Interfaces”?

Compare and use type aliases and interfaces. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Type Aliases and Interfaces” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generics
  2. Type Aliases and Interfaces
  3. Utility Types
  4. Type Guards
← Back to TypeScript Academy