-
Notifications
You must be signed in to change notification settings - Fork 0
Typing Pizza App part 5
A way in which we could add more type safety to our pizza application is by specifying on our code that an order status can not be "any string" (as we have it right now), but also specifying the literal values that the status can be assigned to.
The snippet below shows how it's possible to achive the aditional layer of type sefety to the order status:
type Order = {
id: number;
pizza: Pizza;
status: 'ordered' | 'completed';
};Notice tough, that this would now cause an type error for when a new order is being created, and that can be fixed by setting the correct type to newOrder:
const newOrder: Order = { id: orderId++, pizza, status: 'ordered' };Those notes were written while watching the tutorial videos while taking the classes from the online course Learn TypeScript on Scrimba.
Because english is not my mother language, they can contain some typos and everything written here is based on my understanding about the discussed topics and may not be 100% accurate.
If you want the full course, support the instructor by buying their course on Scrimba.
- Home
- Introduction
- Introduction to TypeScript
- The Pizza Application
- Move to TypeScript
- Defensive Coding
- Typing variables
- Typing Pizza App: part 1
- Custom types
- Typing Pizza App: part 2
- Nested Object types
- Optional Properties
- Typing Pizza App: part 3
- Array Types
- Typing Pizza App: part 4
- Literal Types
- Unions
- Typing Pizza App: part 5
- Typing Pizza App: part 6
- Typing Pizza App: part 7
- Returning Types
- Typing Pizza App: part 8
- Any Type
- Typing Pizza App: part 9
- Utility Types
- Typing Pizza App: part 10
- Generics
- Typing Pizza App: part 11