2 ms·
It isn't redundant in terms of the amount of code written, to me. The overhead of having the utility function and type is greater IMO. If it's in terms of perf
by benatkin 4y ago
It isn't redundant in terms of the amount of code written, to me. The overhead of having the utility function and type is greater IMO.
If it's in terms of performance, that seems like moving the goalposts. I also wonder if it could be optimized away.
Next time I run into it I might use this:
if (Number.isInteger(s)) {
const allowed = (s as number).toExponential()
...and keep the isInteger check close enough that it's readable.
...or this:
if (Number.isInteger(s)) {
const n = s as number // should be optimized away by the compiler I think
- explaininjs 4y agoYou asked for isInteger to work as a type guard and I showed you how. If you prefer explicit casts everywhere that's fine, but it isn't a type guard. You could even use the type anonymously if you really want: (n is number & {Symbol(): never}). Whatever floats your boat, as you say.
- benatkin 4y agoOh boy, I never ever asked for a replacement for Number.isInteger! I prefer to use native functions. Have a nice day.
- explaininjs 4y agoThen alter the type of the ambient native function itself: interface NumberConstructor { isInteger(n: unknown): n is number & { Symbol(): never } }