Description
π Search Terms
isNaN, number, string, javascript
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Since in javascript we can pass a string to isNaN and it works as if it was passed a number, ts validation should accept string as well. Currently it only accepts typeof number.
π Motivating Example
JavaScript's isNaN() function already accepts strings and automatically converts them (like isNaN("123") returns false), but TypeScript currently forces you to manually convert strings first which is annoying and unnecessary. This change would let you pass strings directly to isNaN() just like you can in regular JavaScript, making the code cleaner and more intuitive. It removes a common friction point where TypeScript is being overly strict about something that works perfectly fine in the underlying JavaScript.
π» Use Cases
1
I'm working with user input validation and data parsing where I need to check if string values from forms or APIs are valid numbers. It's super common to get string data that you want to validate before converting to numbers.
2
Right now I have to either cast the string with as any or convert it with Number() first, which is extra boilerplate code. The casting feels hacky and the conversion defeats the purpose since I just want to check if it's a valid number, not actually convert it yet.
3
I'm either doing isNaN(value as any) which TypeScript complains about, or isNaN(Number(value)) which works but feels redundant since isNaN() already does the conversion internally. Sometimes I just write my own helper function but that's overkill for something so basic.