Skip to content

Typing Pizza App part 2

von Schappler edited this page Oct 19, 2024 · 2 revisions

Adding custom type definitions to Pizza App

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.


Using custom types on 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!

Clone this wiki locally