-
Notifications
You must be signed in to change notification settings - Fork 0
Typing Pizza App part 2
When we look into the Pizza App codebase, we can see a few type definitions which could benefit of custom types.
To do so, let's follow this quick workflow:
- We expect the orderId to be a number
- We expect the cashInRegister also to be a number
- Fix the code so it can work properly after adding those type definitions
And with that the basic type declarations should be added correctly into the Pizza App.
To proceed with this, let's understand how each pizza object is constructed:
- It should have two properties, name as a string and price as a number.
With this in mind, it's time then to create our Pizza type:
type Pizza = {
name: string;
price: number;
};Also, after defining the types above, do not forget to add the appropriated type definitions on other parts of the code that may use them.
Just by doing this we are now capable of looking though the code and we notice that there were some other potential TypeScript warnings which are related to the use of cost property when trying to add new Pizzas to your menu.
Fixing this is easy: it's just a matter of replacing cost by price!
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