Options objects
One options param
Long argument lists are fragile because order carries meaning and every new flag shifts all calls. One options object fixes this by naming each input at the call site.
Callers pass only what matters and skip the rest. Readers see labels instead of positions, so intent survives long after the call was written. The program builds a card from one options object and prints the title it used.
interface CardOptions { title: string; wide?: boolean; theme?: string; } function makeCard(opts: CardOptions): string { return opts.title; } console.log(makeCard({ title: "Hello", wide: true }));
Hello
Defaults inside
Optional fields need steady fallbacks in exactly one place. Destructure with defaults at the top of the function so later code works with complete values.
Central defaults keep calls short and behavior stable. Adding a new option later never breaks old callers because the fallback already exists. The program asks for the class of a card with defaults and of one with both options set.
interface CardOptions { title: string; wide?: boolean; theme?: string; } function cardClass(opts: CardOptions): string { const wide = opts.wide ?? false; const theme = opts.theme ?? "light"; return theme + (wide ? " wide" : ""); } console.log(cardClass({ title: "Hi" }), cardClass({ title: "Hi", wide: true, theme: "dark" }));
light dark wide
Partial updates
Settings screens often change one field while keeping the rest. Partial makes every field optional for updates, then spread merges the patch over current state.
The full object stays the source of truth and patches stay tiny. Each update reads as intent: here is what changed, everything else is untouched. The program patches one field and prints the merged object.
interface CardOptions { title: string; wide?: boolean; theme?: string; } function withPatch(base: CardOptions, patch: Partial<CardOptions>): CardOptions { return { ...base, ...patch }; } const next = withPatch({ title: "Hi" }, { wide: true }); console.log(next);
{ title: 'Hi', wide: true }
Readonly options
Shared config should flow down, not mutate along the way. Readonly marks every field as view only so helpers can read options but never rewrite them.
Freezing input at the type level catches stray assignments during review, not in production. Functions return new objects instead of editing what they received. The program reads the title through a function that accepts the frozen type.
interface CardOptions { title: string; wide?: boolean; theme?: string; } type FrozenCard = Readonly<CardOptions>; function titleOf(opts: FrozenCard): string { return opts.title; } console.log(titleOf({ title: "Hello" }));
Hello
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is one options object better than a long argument list?
Answer
Each input has a name at the call site, and its order does not matter
Named fields keep the intent of a call visible and let a new option appear without shifting the others.
-
Where do defaults for optional options belong?
Answer
Inside the function, in one place
One set of fallbacks keeps behavior stable and lets old callers stay short.
-
What does
Partial<CardOptions>allow?Answer
An object with any subset of the fields
Every field becomes optional, which is what an update patch needs.
-
What does
Readonly<CardOptions>stop?Answer
Assignments to the fields of the object
The fields become view only, so a helper cannot rewrite shared configuration.
-
What does this program print?
interface CardOptions { title: string; wide?: boolean; } function makeCard(opts: CardOptions): string { return opts.title + (opts.wide ? " wide" : ""); } console.log(makeCard({ title: "Hello", wide: true }));Answer
Hello wide
The title joins the suffix because the wide flag is true.
-
What does this program print?
interface CardOptions { title: string; theme?: string; } function cardClass(opts: CardOptions): string { const theme = opts.theme ?? "light"; return theme; } console.log(cardClass({ title: "Hi" }), cardClass({ title: "Hi", theme: "dark" }));Answer
light dark
The first call has no theme and takes the fallback, and the second uses the given one.
-
What does this program print?
interface CardOptions { title: string; wide?: boolean; } function withPatch(base: CardOptions, patch: Partial<CardOptions>): CardOptions { return { ...base, ...patch }; } console.log(withPatch({ title: "Hi" }, { wide: true }));Answer
{ title: 'Hi', wide: true }
The spread keeps the base field and adds the patched one.
-
What does this program print?
interface CardOptions { title: string; wide?: boolean; } const base: CardOptions = { title: "Hi", wide: false }; const patch: Partial<CardOptions> = { wide: true }; const next: CardOptions = { ...base, ...patch }; console.log(next.wide, next.title);Answer
true Hi
The patch overwrites wide, and the title comes from the base object.
-
What does the compiler say about this program?
interface CardOptions { title: string; } function makeCard(opts: CardOptions): string { return opts.title; } console.log(makeCard({ wide: true }));Answer
error TS2353: Object literal may only specify known properties, and 'wide' does not exist in type 'CardOptions'.
The object misses the required title and carries a field the interface does not know.
-
What does the compiler say about this program?
interface CardOptions { title: string; } type Frozen = Readonly<CardOptions>; const card: Frozen = { title: "Hi" }; card.title = "Bye";Answer
error TS2540: Cannot assign to 'title' because it is a read-only property.
The readonly type refuses the assignment, which is the point of freezing shared options.
Article author: Arthur Isaev