Skip to content
dreamcode
dreamcode
Map
Interfaces
Lesson 6 of 26
+15 XP on finish
TYPESCRIPTChapter 1 · TS Basics

Describing objects with interfaces

An interface names the shape of an object: which properties it has and their types. Annotate a value with the interface and the compiler enforces the shape.

Worked example

How it reads

  • interface Cloud { ... } names a reusable object shape
  • isFluffy? marks the altitude-related field as optional
Cloud tip: Use optional properties with a question mark to allow fields to be omitted safely.
index.ts
TYPESCRIPT
type-checked, then run in your browser
Console
Run your code to see its output here.
YOUR TURN

Add an optional constellation?: string to Star, create deneb with the constellation "Cygnus", and log deneb.constellation.

Press Run to check your work.