- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
Optional Properties (?)
This lesson covers optional properties โ properties inside an interface that may or may not be present. It's for anyone searching "TypeScript interface optional."
Adding ? after a property name makes that property optional. When it's omitted, its value is treated as undefined.
The example makes a nickname (nickname) optional with interface Profile { name: string; nickname?: string; }, and shows a fallback message with a ternary operator when there's no value.
A common early mistake is accessing an optional property without checking for undefined first. When handling a property that "might or might not have a value," it's important to always build the habit of checking whether it exists.
In real projects, optional properties come up frequently when handling data with a lot of non-required fields, like a form submission.
๐งช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
