3 ms·
I think #6 (optional properties) is a pretty contrived and unrealistic example. Most people use optional properties for extra options or enhancements, not as su
by nepeckman 6y ago
I think #6 (optional properties) is a pretty contrived and unrealistic example. Most people use optional properties for extra options or enhancements, not as substitution for an object hierarchy.
- jgwil2 6y agoI've definitely used them in the way the article describes. Think about an API that gives a list view and then a detail of an object that has more properties than are sent in the list view (maybe because they are expensive to compute or maybe just to save bandwidth). This scenario can definitely be more accurately modeled with interface inheritance than with optional properties.
- nepeckman 6y agoOkay your answer is more illustrative to me of the problem and solution. Totally agree, if you've got a list view and detail view those should be separate classes. Though my personal take in that senario is you might be better off using TypeScripts mapped types to define a subset of your detail view as a list view.
- drew-y 6y agoAgreed. The author is conflating discriminated unions with optional properties. His example is better off as a discriminated union because the optionals are set based off of it's type. There are plenty of situations where optional properties do make sense. Particularly where a default value can be used.
- deleted 6y ago[deleted]