adobe/react-spectrum

Use `null` instead of `Number.NaN` in NumberField component

Open

#6,971 opened on Aug 28, 2024

View on GitHub
 (5 comments) (9 reactions) (0 assignees)TypeScript (1,500 forks)auto 404
enhancementhelp wanted

Repository metrics

Stars
 (15,634 stars)
PR merge metrics
 (PR metrics pending)

Description

Provide a general summary of the feature here

Replace use of Number.NaN values in the NumberField component with null. This would also enable passing in null to the NumberField which was requested in: #5524.

🤔 Expected Behavior?

The expected behavior is that one must check for null before interacting with values returned by a NumberField. This isn't extra work, because one must already call Number.isNaN() when handling number values.

Why is Number.NaN less desirable? Because:

  • TypeScript doesn't error if a developer forgets to check for NaN
  • TypeScript can't determine whether a NaN check was already performed (extra unnecessary checks)
  • Serializing a NaN to JSON resolves to a null value anyway

😯 Current Behavior

All values from a NumberField component need to be checked for Number.isNaN().

💁 Possible Solution

No response

🔦 Context

I observe most of my team members override onChange and value properties to support null instead. This leads me to think that the DX of the NumberField component isn't quite right. Developers don't expect a number to not be a number and TypeScript doesn't warn about it.

💻 Examples

No response

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Contributor guide