Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some Angular objects do not have a value for a property

I am building an app in Angular2 and have a question about how to structure my main class.

I need to create 78 instances of a class. The catch is that not all 78 share the same properties. They share everything but one property. Let's pretend this property is colour. Half of them have a value for colour, half of them don't.

Would you:

A) Divide them into two classes? This seems clunky to me

B) Use a colour decorator and pass those with a colour through the decorator? I think this is the right solution, but I need clarification

C) Something I haven't thought of?

like image 388
Solona Armstrong Avatar asked Jun 06 '26 15:06

Solona Armstrong


1 Answers

Can you declare colour as optional? Declare it as something like colour?: string.

See https://www.typescriptlang.org/docs/handbook/interfaces.html for some more info.

like image 77
EvanM Avatar answered Jun 09 '26 05:06

EvanM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!