type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

`NaN` problems

Open frontsideair opened this issue 3 years ago • 0 comments

Hi, I just found out about this project while writing my own Jsonify helper.

I wanted to document a few limitations regarding the unfortunate situation with NaN. Maybe this can be added to the readme.

Unfortunately, NaN is not a TypeScript literal and cannot be detected at the type-level. I'm surprised that you found a way to detect Infinity and -Infinity so I still have some hope. :)

The affected helpers are Jsonify and Finite, while this list may not be exhaustive.

In case of Jsonify, a NaN value is encoded as null a number can be incorrectly encoded as number when it's a NaN.

In case of Finite, since NaN is not included in finite numbers (according to Number.isFinite check), it can also return an incorrect type in case of NaN.

For context, I needed a Jsonify utility to have typed loader data in Remix, after a JSON encode/decode chain. I also opened an issue (https://github.com/microsoft/TypeScript/issues/48697) in TypeScript repo to request a first-class helper type.

I hope this issue will be helpful and I can try to help in any way I can.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

frontsideair avatar Jun 25 '22 12:06 frontsideair