You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With func2, the return type error is very helpful especially in writing large functions with many branches.
With func, I have more or less the same need for dynamic return type (someone else wrote this unfortunate code and I'm just trying to type it), but TypeScript is unable to warn me that when param2 is a string, I'm actually returning the wrong type.
I read #13225 and got a sense that in a function with overloading, TS only checks if the return type satisfies the return type of the implementation function, not any of the overloads, even though TS only recognizes the overload signatures when the function is called. I've tested this assumption with this slightly altered snippet:
This looks plenty scary to me thinking that by using function overloading, I lost a lot of protection from TS inside the function.
🙂 Expected behavior
Just like what happens in func2, in func it's still possible to statically analyze which overloading signature the returning line is in, and an error is provided about the incorrect return type.
The text was updated successfully, but these errors were encountered:
Bodies of functions are only checked against their implementation signatures. Implementation signatures are unsafely checked against overloads by checking whether their return types are bidirectionally compatible. It's "working as intended" but it's a bit of a design limitation because doing anything better would require some sort of deeper higher-level reasoning that we're not yet equipped for.
Bug Report
🔎 Search Terms
"typescript overloading doesn't check return type"
🕗 Version & Regression Information
[email protected] is my current version. I've only noticed the problem today.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
With
func2
, the return type error is very helpful especially in writing large functions with many branches.With
func
, I have more or less the same need for dynamic return type (someone else wrote this unfortunate code and I'm just trying to type it), but TypeScript is unable to warn me that whenparam2
is a string, I'm actually returning the wrong type.I read #13225 and got a sense that in a function with overloading, TS only checks if the return type satisfies the return type of the implementation function, not any of the overloads, even though TS only recognizes the overload signatures when the function is called. I've tested this assumption with this slightly altered snippet:

This looks plenty scary to me thinking that by using function overloading, I lost a lot of protection from TS inside the function.
🙂 Expected behavior
Just like what happens in
func2
, infunc
it's still possible to statically analyze which overloading signature the returning line is in, and an error is provided about the incorrect return type.The text was updated successfully, but these errors were encountered: