Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback arguments are inferred as any when function has multiple overloads and callback arguments are typed as tuple #41709

Open
markostanimirovic opened this issue Nov 27, 2020 · 4 comments
Labels
Needs Investigation This issue needs a team member to investigate its status.
Milestone

Comments

@markostanimirovic
Copy link

TypeScript Version: 4.0.5 or higher

Search Terms: function overloads, tuples

Code

function f(callback: (...args: [number, string]) => void): void;
function f(callback: (...args: [number, string, boolean]) => void): void;
function f(callback: (...args: any[]) => void): void {}

// demo
f((num, str) => {}); // first signature: ok

f((num, str, bool) => {}); // second signature: all arguments have any type

Expected behavior: num, str and bool should be type of number, string and boolean.

Actual behavior: num, str and bool are type of any

Playground Link:
https://www.typescriptlang.org/play?ts=4.0.5&ssl=3&ssc=32&pln=3&pc=35#code/GYVwdgxgLglg9mABMAFBAhgG0wI3RAawC5EUA6C9AJwHMBnEgbTBAFscBTKgGkTqiowwNALoBKRAF4AfIgBucGABMxJBcoDcAKFCRYCZGiy58xUhTLV6TFuy69+g4bxxw4mDujDips9SrVFJW1daHgkVAxsPEISckpaBkQvAE9GHxl5INUs5UQAbwBfLS0AelLEJQ5WOB0UFFsHAQlMorENRHLkGCp+PhgaMHQoECoOEjgCEtQGtiaeRFd3Ftk2jq66DggEJX7B4dHx5Oxk2jYOMCg6RAALdDkOZLAUxCgUgAcOLSA

@markostanimirovic markostanimirovic changed the title Callback arguments are inferred as any when function has with multiple overloads and callback arguments are typed as tuple Callback arguments are inferred as any when function has multiple overloads and callback arguments are typed as tuple Nov 27, 2020
@MartinJohns
Copy link
Contributor

Overload signatures should go from most specific to least specific. You should change the order of the overloads, then it works.

@markostanimirovic
Copy link
Author

Overload signatures should go from most specific to least specific. You should change the order of the overloads, then it works.

No, then signature function f(callback: (...args: [number, string]) => void): void; will never be reached.

When you change the order and call f((num, str) => {}); it will point to the function f(callback: (...args: [number, string, boolean]) => void): void; signature.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Dec 4, 2020
@RyanCavanaugh
Copy link
Member

I'd need a more realistic example to comment on what the expected behavior is here. The first overload is doing nothing; there's no reason that I can see to not just have the 3-tuple one.

@markostanimirovic
Copy link
Author

@RyanCavanaugh
Here's a more realistic example:

https://www.typescriptlang.org/play?#code/PQKhCgAIUgVBPADgUwM5RMcyAeiD2ATgC6TFLKQDKyANsgMbFEA8VxAhscgDSQBKaAK61iAPkgBeSAApUnbgC5qC5AEopEwahHEA3NjxFS5FNTqNmhAOoBLYgAsACoXyJUbVXxdvUfbboS0jJQkGHyXMjK7JE8oWGIru7KPu7gGpJawqIGhgQkZBSQAKI4xIQcTDHcAGKuALY09ExEHrBBcJC43AB2ACao5s1WLPGQtj0AZsiEKrFjHD3w4GIA2gC6YwD8c9xjykI9ANY9+ADuPQa4+SZFpeWVxNXIdfiNFi2EqHaOqW0dsC6ZWQ-UGTUsRB+ziSHjGE2ms2ecTCYUW8GRKLRKw2212yH2kEOJ3Ol3AeWMhTM9wqVVotgYLwa4M+LHaUk63RBAyGEMILDRfHhM2odIZYlxVFF+JRkAOx1OFyuRgKpko1MekvpjLezKsUL+rIBQN63N1kPs0N8oxlAoWSwxYSFiKlKwlLplcuJirJ1wpqsgABkOPJDezAZzQZBVgA6WNOyAAfUFU2FQfk60gOzTpE9CtJ5JVdzKNOIBrNX28rgAVryAIKEADmqA65cGEdNHxGdvR3exmxlWeDxBYPhrn3rTfFHp5nzbwMj5f1MP59vGKdmfz4aLW-ZROz+BKJedyoAg0EgNUOTFs+B6GCwvoKkyvxBvPUgDEIyEi5etKNbxpcmCnasEihLyiSO4OiKWpziagwAN5jDKqwAJJrpARzIPA+CTDOVioOsyjqrSWqvO8wygSBXxoesYgGDKAC+0EBKI0HPOyJFPKo5Gtmw1HNisIQyrG0YcI2qDKDGsaoAJ0QCXwiT4GOVjKDIomoFKkkwQyqAZFkOiiJsajyZRfJgax4hKjckDPj0163h+X4-tRf5hAB7bAWZS5WmBm7gV6PRQWMo51hJgGRqsyEohpWnKEhMqJSiaEYVhOF4a2RElMWGpSrx1E+e4-FmagtH0dFYTMRVkBKckkAHjK6wsdkxDsaonE5VUPFMgJhUeK2YjQX8HUPEwZYKfV1ZhZOQljKJ4lNlJ1UaXJ+GtNBMpKSpRBqaJW3TdpoXjhJ+kCC1YzGaZvJ9Z4sSTb4-gteVj6kHZDnvp+37cOW6mxgmyhohsJmQGikAIZAjFkqeGDZRw9SIPQ97gBM3CEJMlSUBxCUopM+D4Mo8iEBMDYGJD4DQ+e5aQGcFr4EIpC1ZJMOoQz+Ao4MzBkA4lBEw2DikKgtgNj0XBCF+0aQNYRBHIMkwTMg0ZIwwt7yJAsnDDUePsp9LlmcJKJyKo0SqKdETcNGuP4HwwDAGr1FjDIp0AAzW7b6u8g7lt8D0Qj1KdMjg17kA+-UENqK7NVTZ86S5O7TCa-gAe2XjygAOQAEbianYcnmAMNUzTjiR74kAAIyKMzrPs2Q+BczzQv82rQsi8QYsK5L0uy-L1Pc++jPjIMfSMLQ4nIH0GFx1YivQFgys9Krk8J31pfa8533UfrYSG5Ext3Yzygh+nMz+4HKdq6oFt43wmeEMo-cANRl2HEeT0QDvOy-9syjIlsAELid7X2J9k74H-oQQBodGLh0gDbYu21CAxzJIvPGy8k6WzTjfbOzEy5qFzmeGABcLTF3cJAAATBXc8LMapsx6MQDmtdHD1z5gLZuotxYwwACL4DQD0VOpAzjS1st3M4vdiGDFsIPYeo9x4TDgbyZQlsQb9GDr7EGX5KSUFwko+A09MDgDngvTsS8LR-FIavL6yAfoOzNlEPEpsL5B1ga-BB38P4wLdl-A2QcQ6KRhModOeN6CLGAd43219xJ3xhM-dxcjo64KQUYlBJiYSkLQWfDOWcIZ8HRrQWS8SgA

Check the error on line 117.

@RyanCavanaugh RyanCavanaugh added Needs Investigation This issue needs a team member to investigate its status. and removed Needs More Info The issue still hasn't been fully clarified labels Dec 4, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants