Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 3.3Interfaces and object shapes

Interfaces name the shape expected from an object. They are compile-time descriptions, not JavaScript classes or values created at runtime.

Interfaces

TypeScript
interface User {
  id: number;
  name: string;
}

const user: User = { id: 1, name: "Ada" };

TypeScript uses structural typing: an object is suitable when it has the required shape, even if it was created elsewhere.

Optional properties

Add ? when a property may be absent. With strict checking, test it before assuming it is present.

TypeScript
interface User { name: string; email?: string; }
Feedback 👍
Comments are sent via email to me.