ProgramGuru

Optional Properties

Understand Optional Properties

An optional object property may be absent. Accessing it normally produces a value of type T | undefined under strict checking.

What you will learn: the purpose of optional properties, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how optional properties works and where its type rules apply.
  • Predict the exact output of the optional properties example.
  • Diagnose a common issue: Do not write user.

Build the mental model

Optional properties are especially useful for sparse settings and partial input. Differentiate absent from explicitly undefined when that distinction matters: exactOptionalPropertyTypes makes the assignability rules stricter for optional fields.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Optional Properties: explained visuallycity present? → Yes → string → No → undefinedcity present?Yes → stringNo → undefinedFollow the values through the operation
Optional Properties: city present? → Yes → string → No → undefined.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

type Profile = { name: string; city?: string };
const user: Profile = { name: "Leela" };
console.log(user.city ?? "city not set");
user.city = "Pune";
console.log(user.city);

Verified output

city not set
Pune

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

user initially has no city, so ?? chooses the fallback. Assigning Pune creates that property; reading city then prints Pune.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not write user.city.toUpperCase() without narrowing or a fallback. Optional means the property may be absent, not that TypeScript supplies a default.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Make middleName optional on a Person type. Format a full name both with and without it.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What can a read of an omitted optional property return?
Choose one answer. You can try again.

Answer and explanation

undefined.

An optional object property may be absent. Accessing it normally produces a value of type T | undefined under strict checking.

Review the key idea

Optional Properties: An optional object property may be absent.

user initially has no city, so ?? chooses the fallback. Assigning Pune creates that property; reading city then prints Pune.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Readonly Properties →