-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
is.numericString type guarding returns x is string
and messes up typescript
#176
Comments
Xananax
changed the title
is.numericString type guarding returns x is string and messes up typescript
is.numericString type guarding returns Dec 7, 2022
x is string
and messes up typescript
Maybe we could make the type guard more specific using a technique like this: https://github.com/sindresorhus/type-fest/blob/0b78096186dfe255b888513d60538e17e35828ea/source/internal.d.ts#L108-L111 |
Oh that sounds pretty good and seems like an easy change. Want me to PR this? |
I confirm that changing the signature of var numericString: (value: unknown) => value is `${number}`; fixes the problem. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use case:
Typescript assumes
value is a string
and then castsvalue
tonever
:I'm not sure if this is solvable, because strictly speaking the type guard is accurate.
Workaround (for anyone who needs it): recast the value manually:
JS:
TS:
Notably, this recasting is possible in an early return context like my example, but not in an
if...else
orswitch
context, so it is still a bit of a bother.The recasting is also surprising and requires a comment for other teammates that may come across it.
The text was updated successfully, but these errors were encountered: