Skip to content

isNaN should accept string by ts #61841

Closed
@eternalmangekyo022

Description

@eternalmangekyo022

πŸ” Search Terms

isNaN, number, string, javascript

βœ… Viability Checklist

⭐ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions