BeginnerCoder

Lesson 3 — Interfaces

Describe an object's shape

An interface lists the properties an object must have and the type of each. Once a variable is typed with it, TypeScript checks that the object has every required field, and your editor can suggest them as you type.

interface Book { title: string; pages: number }
const b: Book = { title: "Dune", pages: 412 };
// const bad: Book = { title: "Dune" }; ← error: pages is missing

Why it matters: most data in an app is objects (users, orders, posts); interfaces stop you from forgetting or misspelling a field.

Want it another way?
Task

Add age to the User interface and to the object so the program prints Alex 20 (not Alex undefined).

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.