TypeScript Foundations·Types that pay for themselves

Interfaces and type aliases

Describe object shapes once and reuse the contract across your application.

Intermediate 13 min read Lesson 7 of 300 Challenge included
What you will be able to do
  • Use optional properties for genuinely absent data, not for lazy modelling.
  • readonly prevents accidental mutation at the type level.
  • Interfaces can extend other interfaces to compose contracts.

In plain English

Interfaces and type aliases both describe shapes. Interfaces are open to declaration merging and read naturally for objects; aliases can describe unions, tuples and primitives. Pick one convention and stay consistent.

models.tsTypeScript
interface Lesson {
  id: string
  title: string
  minutes: number
  published?: boolean
}

interface Course extends Lesson {
  readonly slug: string
  lessons: Lesson[]
}

const html: Course = {
  id: 'html',
  slug: 'html',
  title: 'HTML Foundations',
  minutes: 120,
  lessons: [],
}
Your turn

Define a credential

Easy

Create a Credential interface with id, holder, program and an optional score. Then create one valid object.

TS · starter
interface Credential {
  // add fields
}
Check your understanding before moving on.
Knowledge checkQuestion 1

Which declaration can describe a union of string literals?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Unions and narrowing
Lesson 7 of 300 · 0% of course