DEV Community

Discussion on: 4 Must-Know TypeScript Tips & Tricks

 
sam_piggott profile image
Sam Piggott

I think I understand - so you're suggesting if we renamed size to count, this would cause issues?

Type-checking would catch that (the beauty of TypeScript!). In the below example, we pick the two properties to create the new Pizza type...


type Pizza = Pick<Consumable, "size" | "caloriesPerServing">;

const pepperoniPizza: Pizza = {
    size: "large",
    caloriesPerServing: 500
}

Enter fullscreen mode Exit fullscreen mode

But if we renamed "size" to "count", as you suggested...


type Pizza = Pick<Consumable, "size" | "caloriesPerServing">;

const pepperoniPizza: Pizza = {
    count: "large",
    caloriesPerServing: 500
}

Enter fullscreen mode Exit fullscreen mode

Then we would receive a type error at compile time on the pepperoniPizza declaration line, as the object's shape (Pizza) does not have the property count.

We could then perform the changes we require on our type:

type Pizza = Pick<Consumable, "count" | "caloriesPerServing">;
Enter fullscreen mode Exit fullscreen mode

...which would resolve the issue.

Did that make sense? Hope that answers your question!

Thread Thread
 
vetras profile image
vetras

Did that make sense?

yes it does

Hope that answers your question!!

yes it does

thanks!

Thread Thread
 
sam_piggott profile image
Sam Piggott

Ah, that's great to hear. Sorry I misunderstood you initially! :)