1. Concept library
  2. JavaScript & TypeScript
  3. Module 3: Objects, interfaces, unions

Concept

Literal types

A literal type is one exact value: "GET", 404, true. A union of literals is the lightest way to express a fixed set of options, and the compiler catches every typo.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a literal type is and why a union of literals catches typos
  2. Write a type for a fixed set of options and use it in a function signature
  3. Spot where a property widened to string and fix it with as const

In practice

A few lines that show Literal types doing its job.

TypeScript
type Method = "GET" | "POST" | "DELETE";

function send(method: Method, url: string) { /* ... */ }

send("GET", "/users");
send("get", "/users");   // error: '"get"' is not assignable to 'Method'

const config = { method: "POST" } as const;   // method: "POST", not string
send(config.method, "/users");

The common mistake

Watch for

Building an options object with let or without as const, then passing it on. The property widens to string, and the call to the function expecting the literal union fails.

What to know first

Your roadmap puts these before Literal types, unless you already know them.

How it stays learned

Once a session shows you understand Literal types, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Literal types.

Previous in the module← Union types