Skip to content
dreamcode
dreamcode
Map
Assertions
Lesson 16 of 26
+15 XP on finish
TYPESCRIPTChapter 3 · TS Advanced

Intersections and Assertions

Intersection Types (A & B) combine multiple types into one. Type Assertions (as Type) tell the compiler that a value has a specific type, bypassing regular type inference.

Worked example

How it reads

  • Named & Aged combines properties of both interfaces
  • as string forces the compiler to treat rawData as a string
Cloud tip: Use assertions sparingly: they tell the compiler 'trust me, I know what I am doing' and can hide real runtime errors if you are wrong.
index.ts
TYPESCRIPT
type-checked, then run in your browser
Console
Run your code to see its output here.
YOUR TURN

Create type Timed = { startedAt: number } and type Task = { title: string }, make const job: Timed & Task = { startedAt: 5, title: "launch" }, and log job.title.toUpperCase(): LAUNCH.

Press Run to check your work.