Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with IsNumeric when input is whitespace #539

Merged
merged 8 commits into from Jan 13, 2023

Conversation

bconnorwhite
Copy link
Contributor

@bconnorwhite bconnorwhite commented Jan 9, 2023

I'm not actually sure if this is a bug or not, but it at least seems confusing. (Feel free to close if this is expected behavior).

Currently IsNumeric passes with whitespace:

IsNumeric<" ">; // true
IsNumeric<" \t\n\r\f\v">; // true

Instead I've just added a check to ensure that if the type extends `${number}`, it isn't purely whitespace.

@sindresorhus
Copy link
Owner

I agree it's a bug, but I'm not sure the proposed solution is correct. This kinda feels like a TypeScript bug. Maybe worth opening an issue there first?

A fix for this will also need to come with some tests.

source/internal.d.ts Outdated Show resolved Hide resolved
@bconnorwhite
Copy link
Contributor Author

bconnorwhite commented Jan 11, 2023

It sounds like this is intended, here is an issue in the TypeScript repo: microsoft/TypeScript#46109

They're trying to match the behavior of Number("string") such that anything that doesn't resolve to NaN or +/-Infinity is considered valid.

This seems less useful given the intent of this library though. As an example, when designing types for string casing conversions, the distinction between spaces/numbers/etc. is important to know where to split a string into words

@bconnorwhite
Copy link
Contributor Author

bconnorwhite commented Jan 11, 2023

As a side note, because SplitWords parses one character at a time I don't think this actually causes any issues, but "1e2" also passes IsNumeric. There are probably three useful types here:

  • IsNumeric - does the string pass Number()?
  • IsNumber - IsNumeric, but after trimming spaces first
  • IsDigit - Only allow 0-9

Right now only SplitWords is using IsNumeric though, so maybe not worth adding these types

@sindresorhus
Copy link
Owner

It sounds like this is intended, here is an issue in the TypeScript repo: microsoft/TypeScript#46109

I don't think you read the whole thread because the last comment says it's something they want to fix: microsoft/TypeScript#46109 (comment)

/**
Returns a boolean for whether the string is numeric.
*/
export type IsNumeric<T extends string> = T extends `${number}` ? true : false;
export type IsNumeric<T extends string> = T extends `${number}`
? IsWhitespace<T> extends true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that says this is a workaround for microsoft/TypeScript#46109 (comment) ?

| '\u202F'
| '\u205F'
| '\u3000'
| '\uFEFF';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nitpicky, but I prefer the modern escape syntax: \u{7A}.

https://exploringjs.com/es6/ch_unicode.html#sec_escape-sequences

@sindresorhus
Copy link
Owner

IsNumeric - does the string pass Number()?

I don't think the Number behavior is useful. So we should just fix isNumeric.

IsDigit - Only allow 0-9

Could be a useful type, but beyond the scope of this pull request. Should be called IsASCIIDigit as IsDigit would be assumed to include all Unicode digits.

@bconnorwhite
Copy link
Contributor Author

Updated the escape formats.

Also realized that we need to use Trim, otherwise something like this passes:

IsNumeric<" 1 ">

Trim isn't currently accounting for all whitespace characters, so I updated Trim to use the new Whitespace type in internal.d.ts, which should also more accurately match String.prototype.trim.

@sindresorhus sindresorhus merged commit a82342a into sindresorhus:main Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants