Hacker News new | ask | show | jobs
by GMoromisato 206 days ago
Is this a weakness in the type definition? If we're sure the value cannot be undefined, then why doesn't the type reflect that? Why not cast to a non-undefined type as soon as we're sure (and throw if it is not)? At least that would document our belief in the value state.

I may not understand.

2 comments

If your type definitions are airtight then this problem doesn't come up, but sometimes, for whatever reason which may not be entirely within your control, they aren't. Narrowing and failing early is precisely what the article advises doing.
JavaScript isn't even statically typed.

> Why not cast to a non-undefined type as soon as we're sure (and throw if it is not)

This is exactly what the OP author suggests.