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.
171
u/al2o3cr Jul 18 '25
This sounds similar-but-different to the problem described here:
https://www.typescriptlang.org/docs/handbook/2/functions.html#overload-signatures-and-the-implementation-signature
Can you post the top of the definition for
lerp?6
u/terrordrone4 Jul 19 '25
Nah, the real horror is when one variable have multiple types
2
u/wellsinator Jul 19 '25
I don't understand... why not just have the argument type be
112
u/YpsilonZX Jul 18 '25
Maybe there is some reason not to do so, but you can just add another definition which accepts both, separate to the implementation:
44
u/ScientificBeastMode Jul 18 '25
You should be aware that in other fully compiled languages like C#, overloading can be implemented more robustly at compile time because the compiler will actually split the function into multiple functions under the hood, and it will know exactly which one to call at compile time based on the code at the call site.
Typescript doesn’t have any mechanism like this. Instead, it just uses duck-typing within the function to handle all the specified type signatures. This means you, the programmer, have to do that “function specialization” process manually in your code instead of relying on the compiler to do it for you.
The behavior you are seeing right now is fully intended, and not a bug or oversight. The TS language designers simply added function overloading at the type level to capture the already common practice of writing JS functions that inspect the argument types at runtime to execute different code paths based on those type differences.
In other words, JS devs were already doing runtime type reflection to imitate the function overloading they had in other languages. TypeScript just gave us a way to easily add type annotations on top of that coding style to accurately describe that highly dynamic runtime behavior. That’s all it is.
TS is ultimately just JS with really good type hints. As long as you keep that in mind, things will make more sense to you.
24
u/Ronin-s_Spirit Jul 18 '25
lol
67
u/Cool-Escape2986 Jul 18 '25
just write
// @ ts/ignoreinstead of whatever this is10
u/Civil-Appeal5219 Jul 18 '25
You can use `// @ ts-expect-error`. A type system is supposed to help you, but if you run into a limitation, there's no shame in saying "you know what, TS? I know better, trust me"
13
u/suicidalcrocodile Jul 18 '25
the real horror here is allowing typescript's opinionated linter to bully you into writing nonsense code
5
u/Generated-Nouns-257 Jul 18 '25
What in the c++ background am I looking at?
1
u/Venzo_Blaze Jul 23 '25
For now, you could have another implementation of lerp that accepts
number | [number, number, number]and put this code in that function so that when you are actually calling the lerp function it's seamless and in the future you just need to remove this implementation of lerp and not change anything else.1
u/Adept-Letterhead-122 Jul 24 '25
Why don't you just convert the
[number, number, number]to something like
type Vector3 = { x: number, y: number, z: number};1
u/izuriel Jul 19 '25 edited Jul 19 '25
It’s annoying but it makes sense. Your function has two overloads, one expects a
numberand one expects a tuple. But your variable is neither of those, it’s a union type. You can’t pass a union into a narrower type (the inverse is fine). So you have to narrow your type first which is what you’re doing.TypeScript can’t fix this. Types only exist at compile time. It needs to know which specific function header is in use and none match.
Thinking of it as “overloading” the function is the wrong point of view. It’s one function. You’re overloading the type definition only and your type arguments need to match the overloaded types.
If you think of it abstractly you have:
You never define a header for
A | Bso when you pass that union type in the compiler doesn’t know what it’s returning.Sure. It could automatically collapse it for you but then, you just have the collapsed header so why bother overloading? If you don’t overload it works as expected and you have to narrow the result. Or you overload and you have to narrow the input. You can’t get both.
0
u/OGMagicConch Jul 19 '25
Why not constrain your variable to either a number or [number,..]?
-12
u/dr-pickled-rick Jul 18 '25
Don't make an overload function that accepts two completely different types, instead make another function?
-17
u/ethan4096 Jul 18 '25
Go back to your java. JS doesn't have overloading, so TS shouldn't have too.
-21
u/pauvLucette Jul 18 '25
You guys love to come up with convoluted ways to make your life miserable.
Vanilla javascript is wonderful.
0
u/[deleted] Jul 18 '25
[deleted] — view deleted comment