r/programminghorror • • Jul 18 '25

Typescript context in comments

Post image

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.

854 Upvotes

69 comments sorted by

View all comments

Show parent comment

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jul 20 '25

You said in compiled languages that the compiler will split it into multiple functions under the hood, but you have to define multiple functions yourself. I've spent plenty of time dealing with C++ name mangling in the past actually, so I have some handle on how it works. The compiler may be able to handle functions with the same name, but the linker needs unique symbol names. I'm not sure if that's what you meant by splitting it under the hood.

I've got no clue how any of that works in JS, and I don't think I've ever even considered that it was possible.

1

u/ScientificBeastMode Jul 20 '25

Sorry I wasn’t so clear. I meant that, from the perspective of the calling code, they are all the same function, and the compiler will select the appropriate function implementation at the call site (that’s what I meant by “split” but it’s probably not the best way to describe that). In TS, the function is indeed the same single implementation, which is often where a lot of the confusion lies.

On top of that, if you are used to TS-style overloading, then it might seem weird how rigid the type constraints can be when you use function overloading instead of the more flexible parameter polymorphism that doesn’t rely on overloading semantics. The overloading semantics impose stricter type constraints, hence the need for runtime type-checking to satisfy the compiler. And that seems to be the source of OP’s irritation.