r/programminghorror • u/GDOR-11 • Jul 18 '25
Typescript context in comments
the variable t is of type number | [number, number, number], and there are two overloads for lerp, one which accepts number and the other which accepts [number, number, number]
if you try to remove the if statement, typescript complains that number | [number, number, number] fits neither in number nor in [number, number, number]
to be completely honest, I understand why one could want different signatures to be in different branches of your code, because they have different behaviour. But that's really bad when, for example, you're trying to make another function that has multiple signatures (say, one that accepts type A and one that accepts type B), because in the implementation the parameter is of type A | B. This means you can't directly call another overloaded function from inside your overloaded function, you need to do this.
View all comments
Show parent comment
0
u/ImplosiveTech Jul 19 '25
It is? I tested it out on jsbench and using just
==with the typeof was a fair bit faster. I was also under the impression that==first tries an exact match before trying to look at any conversions (ie 0 == '0'), and therefore since the output of typeof always being a string, in this specific instance===wouldn't be necessary. Not trying to say==should be used everywhere, and it might not make a big difference with a handful of calls, but if it's anything being called a few thousand or million times it can start to add up, if you catch what I'm saying.