-
-
Notifications
You must be signed in to change notification settings - Fork 532
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
Conversation
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. |
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 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 |
As a side note, because
Right now only |
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) |
source/internal.d.ts
Outdated
/** | ||
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 |
There was a problem hiding this comment.
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) ?
source/internal.d.ts
Outdated
| '\u202F' | ||
| '\u205F' | ||
| '\u3000' | ||
| '\uFEFF'; |
There was a problem hiding this comment.
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
I don't think the
Could be a useful type, but beyond the scope of this pull request. Should be called |
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 |
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:Instead I've just added a check to ensure that if the type extends `${number}`, it isn't purely whitespace.