typeover
curriculum

Curriculum Basics Optionals (?T) exercise 3 · mcq

Optionals (?T)

TypeScript's if (x !== null) narrows the type so the body sees a non-null value. Zig has a built-in form for this: if (x) |y| { ... } — the body runs only when the optional is present, and y is the unwrapped value. Pick the Zig translation.

TypeScript reference
Pick the idiomatic Go translation

About this theme

TypeScript uses T | null (or T | undefined) to mark "maybe absent" values; Zig's analogue is the type prefix ?T. A ?T value is either some T or null. Three idiomatic ways to handle one: orelse <default> (TS ??), if (x) |unwrapped| { ... } (payload capture that runs only when present), or x.? for "I know it's not null, unwrap anyway" (panics on null). No more silent nullable bugs.