Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure, but in that case string | undefined is the correct type, and turning it into string | “” isn’t helping anybody.

It’s the difference between:

if (fooBarDisplayName) { show div }

And:

if (foobarDisplayName && foobarDisplayName.length > 0) { show div }

Ultimately, type systems aren’t telling you what types you have to use where - they’re giving you tools to define and constrain what types are ok.



I'm not sure I follow the point you're making

My example is we want to skip the div if empty or undefined. We can't throw on assignment so we leave it as as string|undefined.

When we go to display, we have to check if the string is empty anyway, right? What if it's empty in the DB or API response?

No matter what the display-component is doing something to prevent showing the empty string.

` if(fooBarDisplayName.length) { show div } `

or

` if(fooBarDisplayName?.length) { show div } `

I'm not sure what we gain by leaving it as `string|undefined`

---

If there was a "NonEmptyString" type maybe I could see where you're coming from.

I guess you could argue treating `string` like a `NonEmptyString` type seems error prone. The compiler can't verify you've done the check. At some point someone will set a string when it's empty or not do the necessary check.

You'd want a separate non-string type to make sure it's actually been parsed




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: