TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
TypeScript / Chapter 26 of 60~12 min • Beginner
Chapter 26TypeScript

Optional properties and null safety

Optional properties may be absent. With strictNullChecks, code must account for undefined and null where they are allowed. Optional chaining stops when its left operand is nullish, and ?? provides a fallback only for null or undefined.

How it works

Step-by-step walkthrough

  1. Define an optional field
  2. Create an object without it
  3. Provide a fallback

Example

TypeScript
type Settings = { baseURL?: string };
const settings: Settings = {};
console.log(settings.baseURL ?? 'http://localhost:3000');
Expected result: http://localhost:3000
💡 Point to remember
Avoid non-null assertions unless the invariant is truly guaranteed.

✎ Your practice task

Set baseURL to a different value.

Knowledge check

Which operator supplies a nullish fallback?

🌐 Continue learning