facebook / fbjs

A collection of utility libraries used by other Meta JS projects.
MIT License
1.95k stars 313 forks source link

nullthrows has either bad condition or bad error message #506

Closed hejtmii closed 1 year ago

hejtmii commented 1 year ago

Hi, while debugging DraftJS, I found out that the function nullthrows checks only for null, but its error message says "Got unexpected null or undefined"

Also, the way its result is typed seems wrong, because undefined passes the condition returning undefined where non-nullable T should be ...

I would expect it to either check also for undefined, or return T | undefined and not return undefined in the error message ...

https://github.com/facebook/fbjs/blob/d8f0f430a689ee1e5bb070d501155fa29e329019/packages/fbjs/src/utils/nullthrows.js#L15

bigfootjon commented 1 year ago

I found out that the function nullthrows checks only for null

That's not correct, the function does a != null check which means that null and undefined are both considered not equal to null. If the function instead used !== null then you would be correct.

i just double-checked in the JS console in Chrome to be sure:

null != null false undefined != null false 0 != null true

I suspect the problem is somewhere else.