Comments (4)
I confirm that changing the signature of numericString
like so:
var numericString: (value: unknown) => value is `${number}`;
fixes the problem.
from is.
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
from is.
Oh that sounds pretty good and seems like an easy change. Want me to PR this?
from is.
Use case:
const parseValue = (/**@type {string} */value) => { if (is.numericString(value)) { return parseFloat(value); } if (value.toLowerCase() === "true" || value.toLowerCase() === "false") { return value.toLowerCase() === "true"; } //... }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:
value = /**@type {string}*/(value)TS:
value = value as stringNotably, 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.
Hi!
The problem is that in a predicate function, the type guard must always be accurate: if the predicate on a given value returns true, the value must be of the given type in the type guard AND if the predicate returns false, the value must not be of the given type in the type guard.
And this is not the case in the is.numericString predicate. So the solution is indeed to modify the type guard to use a type representing all the numeric string values.
from is.
Related Issues (20)
- Property 'function' does not exist on type 'typeof is'. HOT 1
- Proposal: is.nonEmptyStringAndNotWhitespace(value) HOT 10
- [v5] Better handling of `NaN`
- test code perf HOT 1
- Can't access default export properties when `moduleResolution: 'NodeNext'` HOT 3
- Add is.renderInReact or is.notRenderInReact HOT 1
- Question: What is the reason Node.js below version 14.16 is not supported? HOT 2
- add type inference to "nonEmptyArray"
- Confused between class_ docs and implementation HOT 1
- How to shorten: `"prop" in obj && obj.prop`
- Improve type narrowing for `nonEmptyArray` HOT 1
- Parcel JS build issue - No Main HOT 1
- v5.4.0 "Cannot find module '@sindresorhus/is' or its corresponding type declarations" HOT 4
- `plainObject` is incompatible with `structuredClone` HOT 2
- Some type guards under assert are not working HOT 1
- directInstanceOf(instance, class) throws error if instance is null or undefined HOT 1
- Struggling to configure jest to compile `is` HOT 1
- is.enumCase method is not a type guard HOT 2
- Typing errors with the isEmptyStringOrWhitespace type guard HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from is.