Crash Courses · Basic
Interfaces & Type Aliases
Define the shape of objects with interfaces and type aliases.
10 min read+150 XP on completionCert: TypeScript Crash Course
Tap any word in the text below to start reading from there.
Interfaces & Type Aliases
TypeScript interfaces and type aliases define the shape of objects — the contract that code must satisfy.
Interface
interface User {
id: string
name: string
email: string
role: 'admin' | 'student'
bio?: string // optional
}
const user: User = {
id: 'user_001',
name: 'Jordan Morris',
email: 'jordan@jsupremetech.com',
role: 'admin',
}Type Alias
type User = {
id: string
name: string
}
// Type aliases can do things interfaces cannot
type StringOrNumber = string | number
type Callback = (err: Error | null, result: string) => voidExtending Interfaces
interface Entity {
id: string
createdAt: Date
}
interface Course extends Entity {
title: string
xp: number
}readonly Properties
interface Config {
readonly apiUrl: string // cannot change after creation
debug: boolean // can change
}
const config: Config = { apiUrl: '/api', debug: false }
// config.apiUrl = '/other' // Error: read-only
config.debug = true // OKIntersection Types
type AdminUser = User & { permissions: string[] }
type BaseEntity = { id: string } & { createdAt: Date }Reading progress
0% read
In this module
interface
type alias
Optional property
readonly
0%