This course builds on TypeScript intermediate. You should be able to explain generic callbacks, mapped types, Result unions, unknown decoders and compile-time tests. Revisit generic callbacks, array decoders and contract tests if needed. JavaScript advanced is useful extra practice, but not required. No framework or decorator knowledge is assumed.
Before learning a new tool, recall the boundary: a Result success must be narrowed before reading value, and an unknown input needs runtime validation. TypeScript cannot make a server's data true. This week we build a small event gateway whose public types preserve relationships and whose runtime enforces a separate contract.
A type-level decision
A conditional type has the shape T extends U ? A : B. Read it as “if T is assignable to U, choose A; otherwise choose B.” This extends is a type relationship, not class inheritance. The conditional disappears from JavaScript; it cannot branch on an unknown runtime value.
In HasTitle below, the record shape is the test and true/false are literal types. Assigning a value to each result makes the invisible calculation observable to the checker. Predict whether each assignment compiles before running. Prefer a named domain type when there is no useful relationship to derive.
Predict, check, and run
Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.
type HasTitle<T> = T extends { title: string } ? true : false;
const book: HasTitle<{
title: string;
pages: number;
}> = true;
const count: HasTitle<number> = false;
console.log(book, count);Enable JavaScript to edit and type-check this example.
Check your prediction
true falseRead the compiler error
Predict the rejected relationship before checking. Read the real diagnostic and its source location; fix the contract or the value instead of hiding it with an assertion.
type HasTitle<T> = T extends { title: string } ? true : false;
const answer: HasTitle<number> = true;
console.log(answer);Enable JavaScript to edit and type-check this example.
Check your diagnosis
The key line is TS2322: Type 'true' is not assignable to type 'false'. Read it as supplied → required: you supplied the literal true, and the required type is what HasTitle<number> evaluated to. Seeing false there tells you which branch the conditional took.
The number type does not satisfy the record requirement, so the result is the literal false, not boolean.
Why a library needs this
HasTitle only answers true or false, which you rarely need. The conditional types you will use every week are in the standard library, and they derive one type from another. Two of them:
Extract<T, U>keeps the members of a union T that are assignable to U. Its definition is one conditional type:T extends U ? T : never.NonNullable<T>removes null and undefined:T & {}in today's library, with the same effect as a conditional that maps null and undefined to never.
type AppEvent =
| { type: "added"; title: string }
| { type: "removed"; id: string };
type Added = Extract<AppEvent, { type: "added" }>;
type Profile = { id: string; nickname: string | null };
type Nickname = NonNullable<Profile["nickname"]>;
const added: Added = { type: "added", title: "Types" };
function shout(name: Nickname): string {
return name.toUpperCase();
}
console.log(added.title, shout("ada"));Enable JavaScript to edit and type-check this example.
Check your prediction
Types ADAYou could write type Added = { type: "added"; title: string } by hand. It would compile today, and silently drift the day someone adds a field to the added event: AppEvent would change and Added would not. The derived version follows AppEvent automatically, and Nickname follows Profile. That is the job of a conditional type: keep one source of truth and compute the rest. (Extract works member by member on a union; the next lesson explains why.)
Trace the contract before building
Read a conditional type as a question about assignability, not as an if statement executed on a value. First substitute a concrete input type, then choose the branch and simplify the result. There is no emitted runtime check and no validation of incoming data.
Start with one supported type and one unsupported type before trying a union. Keep the resulting type small enough that a consumer can predict it. If the alias becomes difficult to explain, name intermediate pieces and test them separately. The purpose is to preserve a useful relationship at an API boundary, not to replace ordinary JavaScript conditions that still have to run.
Build it yourself
Conditional Types: practice
Write IsList<T>, returning true for readonly arrays and false for other inputs. Check a readonly string list and a number, then print true false. Explain why this is not an Array.isArray replacement.
// Start from the stated contract. Add a valid call and an edge case.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
Test assignability to readonly unknown[] so both mutable and readonly arrays qualify.
Hint 2: a first step
The two branches should be literal types true and false.
Show one solution
type IsList<T> = T extends readonly unknown[] ? true : false;
const list: IsList<readonly string[]> = true;
const scalar: IsList<number> = false;
console.log(list, scalar);Enable JavaScript to edit and type-check this example.
true falsereadonly unknown[] is the widest array type, so both string[] and readonly string[] are assignable to it and choose the true branch; with unknown[] instead, a readonly list would get false. Why this is not an Array.isArray replacement: IsList is evaluated by the checker on a type you already have and is erased from the JavaScript, so it cannot look at a value. For an unknown value from JSON, only the runtime call Array.isArray(value) can find out, and the checker then narrows using its result.
Change it and test it
Try assigning true to IsList<number>. Then write an actual runtime Array.isArray check on an unknown value and explain why the two operations solve different problems.
Completion is saved in this browser only. It does not measure mastery.