This doesn't have to do with classes vs functions. If you have a place that accepts string type values, then later you're trying to pass something that is of type string | string[], that's simply wrong and TS is doing a good job telling you that this is a problem. You might know what you're passing, but the next developer, and the next one after that, won't know. This is about scaling code bases in a way that prevents accidents, classes or not.
Full-time web dev; JS lover since 2002; CSS fanatic. #CSSIsAwesome
I try to stay up with new web platform features. Web feature you don't understand? Tell me! I'll write an article!
He/him
To understand this more specifically, if someone write some piece of code that accepts objects of type Model based on the above class in the article, f.e. some code like
letarray:Model[]=[]// push some models into the array, including Animal// instances, then later:constcol:string=array[2].idColumn
this code is going to fail if it unexpectedly receives string | string[] because it is expecting to have a string only, and may call string-specific methods on that string, etc.
As you can see here, this doesn't have to do with classes vs functions at all. Definitely use only functions if you like that, but this problem still needs to be taken into account, and it is simply about assignment.
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.
This doesn't have to do with classes vs functions. If you have a place that accepts
string
type values, then later you're trying to pass something that is of typestring | string[]
, that's simply wrong and TS is doing a good job telling you that this is a problem. You might know what you're passing, but the next developer, and the next one after that, won't know. This is about scaling code bases in a way that prevents accidents, classes or not.@trusktr Which thing are you referencing? This is a pretty long thread at this point lol
To understand this more specifically, if someone write some piece of code that accepts objects of type
Model
based on the above class in the article, f.e. some code likethis code is going to fail if it unexpectedly receives
string | string[]
because it is expecting to have astring
only, and may call string-specific methods on that string, etc.As you can see here, this doesn't have to do with classes vs functions at all. Definitely use only functions if you like that, but this problem still needs to be taken into account, and it is simply about assignment.