var mostRecent = '';
if (type(c.messages) === 'array' ||
type(c.messages[0]) === 'object' ||
type(c.messages[0].text) === 'string') {
mostRecent = c.messages[0].text.substring(0, 30);
}
return name + ': ' + mostRecent;
.. apart from the fact that he probably meant && instead of || in that condition, we've also now got a system that silently swallows errors. Bugs can lurk in it without any more symptoms than producing empty strings where more interesting strings were expected.I much prefer my systems to crash when a programmer made an error, and do so noisily and detectably, so that the problem can be fixed. I also prefer the actual logic of my code to make up more than 20% of the lines. If you're going to add boilerplate type checks all over, the signal-to-noise ratio gets really bad.
So don't do this. Use Typescript if you want to check types.