Interfaces and type aliases
Describe object shapes once and reuse the contract across your application.
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.
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
EasyDefine a credential
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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.