I guess if you wanted to add count to the new type, could you do something like so:
count
interface Pizza extends Pick<Consumable, "size" | "caloriesPerServing"> { count: number; }
That way, you'd get size, caloriesPerServing and count as properties on the Pizza type?
Pizza
Unsure if that's what you're asking, but I hope that helps!
true. that is not what I meant. I mean renaming the original property named "size" to the new name "count" (as an example).
Refactoring is a big part of writing maintainable software and renaming is a basic corner stone of refactoring. Hence my question.
I think I understand - so you're suggesting if we renamed size to count, this would cause issues?
size
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 }
But if we renamed "size" to "count", as you suggested...
type Pizza = Pick<Consumable, "size" | "caloriesPerServing">; const pepperoniPizza: Pizza = { count: "large", caloriesPerServing: 500 }
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.
pepperoniPizza
We could then perform the changes we require on our type:
type Pizza = Pick<Consumable, "count" | "caloriesPerServing">;
...which would resolve the issue.
Did that make sense? Hope that answers your question!
Did that make sense?
yes it does
Hope that answers your question!!
thanks!
Ah, that's great to hear. Sorry I misunderstood you initially! :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I guess if you wanted to add
count
to the new type, could you do something like so:That way, you'd get size, caloriesPerServing and count as properties on the
Pizza
type?Unsure if that's what you're asking, but I hope that helps!
true. that is not what I meant. I mean renaming the original property named "size" to the new name "count" (as an example).
Refactoring is a big part of writing maintainable software and renaming is a basic corner stone of refactoring.
Hence my question.
I think I understand - so you're suggesting if we renamed
size
tocount
, 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...But if we renamed "size" to "count", as you suggested...
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 propertycount
.We could then perform the changes we require on our type:
...which would resolve the issue.
Did that make sense? Hope that answers your question!
yes it does
yes it does
thanks!
Ah, that's great to hear. Sorry I misunderstood you initially! :)