Comments (10)
@rayepps did a PR for this
from radash.
It should be as easy as this:
export const isString = (value: any): value is string => {
return typeof value === 'string' || value instanceof String
}
Simply add a TS guard as return type
Reference: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
from radash.
I was thinking about how to implement this.
I wanted to do a PR addressing all the different is* functions, but I have a question, so wanted to discuss here a bit before addressing it.
Given that the different tests do a check with typeof '<primite_type>' || instanceof <Class>
, what should be the type of the variable?
In the example above should it be
export const isString = (value: any): value is string => {
return typeof value === 'string' || value instanceof String
}
or
export const isString = (value: any): value is string | String => {
return typeof value === 'string' || value instanceof String
}
The latter one is more correct, but may be more "painful" to work with, since if you do so then value
becomes value: string | String
so functions accepting string
may complain
Since the case of using the String class are practically non-existing, it may be safer to do the following
export const isString = (value: any): value is string => {
return typeof value === 'string'
}
and then expose a method isClass
to check if is instance of class, what are your thoughts on this?
from radash.
Wow, I have never seen this type predicate before, I didn't know this existed. I'd love to add this to all the isN
functions in the typed module.
@matteosacchetto regarding your question, it seems valid both ways but I'd probably prefer value is string
. For 99% of people I think this will be the happy path. For the 1% who need the value to be a String
instance type they can cast it.
from radash.
So, just to be on the same page, this is the solution you prefer, right?
export const isString = (value: any): value is string => {
return typeof value === 'string' || value instanceof String
}
If so I will proceed and do a PR and add type guards to all isN
methods
from radash.
Yes 👍 it would be awesome if you made a PR but I can also do it. I'd like to get it in for the 8.0.0
release with the ESM change (today or tomorrow)
from radash.
I have a question on the isN
functions:
export const isSymbol = (value: any): value is symbol => {
return !!value && value.constructor === Symbol
}
Why in some functions you didn't use typeof
or instanceof
like for isString
?
from radash.
For each is
check I used the most dependable check I could find on google, stackoverflow, and other libraries. Because Javascript is a quirky language using instanceof
is not always the most reliable way to check the type of a variable at runtime.
A good example is isDate
, as mentioned in this SO post using instanceof
on a Date will fail when passing across frame boundaries.
There are lots of little quirks like that we want to account for.
from radash.
Ok, good to know, thank you for the details 😉
from radash.
Thanks guys I didn't get notifications of this activity or I would have responded sooner.
from radash.
Related Issues (20)
- why not provide deep copy method,be like deep copy of array or object? HOT 7
- Incorrect Return Value for isObject(Object.create(null)) HOT 3
- Ts type error in 'shake'
- construct sometimes converts integers with letters into an array instead of an object
- can't open `Edit this page on GitHub` in docs
- AggregateError cannot be used with `instanceof` HOT 3
- We need Chinese documents HOT 5
- Why is cloneDeep not implemented HOT 2
- Feature Request: Convert flat array objects to tree-like data
- crush behaves unexpectedly HOT 1
- Similar to `objectify`, to return `Map` HOT 4
- [Async] Allow onRetry option for retry HOT 1
- isNumber(NaN)
- allow debounce to trigger on leading (and trailing) edge HOT 3
- `isObject` should not be used by other functions in radash HOT 1
- debounce 函数在vue3 选项式api中使用存在this指向问题
- [REQUEST] assign - How to keep object references
- [Array] toggle with falsy elements returns wrong result HOT 1
- `isPromise` misleading in type definitions, it should return `value is PromiseLike<>` HOT 1
- [Request] Loosen the type on the Array sum function HOT 2
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 radash.