🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
📘 TypeScript

Optional and readonly

📚 What are Optional and Readonly Properties in TypeScript? Optional properties (name?: string) can be present or absent in an object. If the value is not provided, it will be undefined. Readonly properties (readonly id: number) can be set when the object is created but cannot be changed afterwards…

8 min 10 XP Lesson 8 of 21
Optional and readonly
🌐

Appy Says…

Not every field on an object is required. A user might not have a profile picture. An event might not have an end time. TypeScript lets you mark properties as optional — and then reminds you to handle the case where they're missing.

📖

What are Optional Properties?

In TypeScript interfaces and types, adding ? after a property name makes it optional — the property may or may not exist on the object.

  • interface User { name: string; avatar?: string; }
  • Optional means the value is string | undefined
  • Access safely: user.avatar?.toUpperCase() — optional chaining
  • Nullish coalescing: user.avatar ?? '/default.png'
  • TypeScript errors if you use optional property without handling undefined
  • Different from null: optional means the key may be absent; null means the key is present but empty
🎮

Think of it like optional Minecraft enchantments

A sword always has a name and damage stat. But enchantments are optional — a sword might have Sharpness or it might not. TypeScript optional properties model exactly this: required fields always there, optional ones might be absent.

⚙️

How It Works

  • 1. Mark optional: interface Config { timeout?: number; retries?: number; }
  • 2. Create without optional: const config: Config = {}; — valid
  • 3. Access: config.timeout — type is number | undefined
  • 4. Guard: if (config.timeout !== undefined) { ... }
  • 5. Optional chaining: config.timeout?.toString()
  • 6. Default: const timeout = config.timeout ?? 5000;
🌍

Real-World Examples

  • User profile: { name: string; bio?: string; website?: string; }
  • API response: { data: T; error?: string; }
  • React component props: { label: string; icon?: React.ReactNode; onClick?: () => void; }
  • Database record: { id: number; deletedAt?: Date; }
💡

Key Facts

  • Optional properties exist only in TypeScript — JavaScript has no concept of required vs optional object keys
  • Required<T> utility type removes all ?; Partial<T> makes all properties optional
  • Optional chaining ?. and nullish coalescing ?? are JavaScript features that pair naturally with TypeScript optional properties
  • Exact types (non-optional): TypeScript interfaces don't enforce that extra properties are absent — use satisfies or exact type libraries for that
⚠️

Watch Out!

Don't make everything optional to silence TypeScript errors. If a field is truly required for the object to make sense, keep it required. Optional properties should reflect genuinely optional data — overusing them weakens TypeScript's guarantees.

📌

Remember

prop?: Type = the property may be absent (value is Type | undefined). Always handle the undefined case with optional chaining ?. or nullish coalescing ??.

What You Learned

  • Optional properties: name?: string — value is string | undefined; property may be absent
  • Handle with optional chaining ?., nullish coalescing ??, or explicit undefined checks
  • Unlocks: flexible object shapes, partial configs, optional React props, nullable DB fields

Key Facts

  • Optional properties exist only in TypeScript — JavaScript has no concept of required vs optional object keys
  • Required<T> utility type removes all ?; Partial<T> makes all properties optional
  • Optional chaining ?. and nullish coalescing ?? are JavaScript features that pair naturally with TypeScript optional properties
  • Exact types (non-optional): TypeScript interfaces don't enforce that extra properties are absent — use satisfies or exact type libraries for that

Real-World Examples

• User profile: <code>{ name: string; bio?: string; website?: string; }</code> • API response: <code>{ data: T; error?: string; }</code> • React component props: <code>{ label: string; icon?: React.ReactNode; onClick?: () => void; }</code> • Database record: <code>{ id: number; deletedAt?: Date; }</code>

Remember

prop?: Type = the property may be absent (value is Type | undefined). Always handle the undefined case with optional chaining ?. or nullish coalescing ??.

Quick Quiz

1 / 2

name?: string means?