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

Only the last signature of an overloaded function is allowed in Parameters<T> #28789

Closed
calvinlauyh opened this issue Dec 1, 2018 · 3 comments

Comments

@calvinlauyh
Copy link

calvinlauyh commented Dec 1, 2018

TypeScript Version: 3.2.1

Search Terms: overloaded function. overloaded signature, overloaded arguments

Code

function testFn(a: string): boolean;
function testFn(a: number): boolean;
function testFn(a: boolean): boolean;
function testFn(_a: string | number | boolean): boolean {
  return true;
}

interface Stub {
  callsFake(...args: Parameters<typeof testFn>): void
}

const stub1: Stub = {
  callsFake(_a: string): boolean {
    return true;
  }
}
const stub2: Stub = {
  callsFake(_a: number): boolean {
    return true;
  }
}
const stub3: Stub = {
  callsFake(_a: boolean): boolean {
    return true;
  }
}

Expected behavior:
Expected stub1 and stub2 to be compiled without any error.

Actual behavior:

Both stub1 and stub2 shows

Type '(_a: string) => boolean' is not assignable to type '(a: boolean) => any'.
  Types of parameters '_a' and 'a' are incompatible.
    Type 'boolean' is not assignable to type 'string'. [2322]

Playground Link:
http://www.typescriptlang.org/play/#src=function%20testFn(a%3A%20string)%3A%20boolean%3B%0D%0Afunction%20testFn(a%3A%20number)%3A%20boolean%3B%0D%0Afunction%20testFn(a%3A%20boolean)%3A%20boolean%3B%0D%0Afunction%20testFn(_a%3A%20string%20%7C%20number%20%7C%20boolean)%3A%20boolean%20%7B%0D%0A%20%20return%20true%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20Stub%20%7B%0D%0A%20%20callsFake(...args%3A%20Parameters%3Ctypeof%20testFn%3E)%3A%20void%0D%0A%7D%0D%0A%0D%0Aconst%20stub1%3A%20Stub%20%3D%20%7B%0D%0A%20%20callsFake(_a%3A%20string)%3A%20boolean%20%7B%0D%0A%20%20%20%20return%20true%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0Aconst%20stub2%3A%20Stub%20%3D%20%7B%0D%0A%20%20callsFake(_a%3A%20number)%3A%20boolean%20%7B%0D%0A%20%20%20%20return%20true%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0Aconst%20stub3%3A%20Stub%20%3D%20%7B%0D%0A%20%20callsFake(_a%3A%20boolean)%3A%20boolean%20%7B%0D%0A%20%20%20%20return%20true%3B%0D%0A%20%20%7D%0D%0A%7D

Related Issues:
#13570
The closest I can find is this but it has generic types at the first function's signature, I am not sure fundamentally do they refer to the same issue.

@calvinlauyh calvinlauyh changed the title Only the last signature of an overloaded function is return in Parameters<T> Only the last signature of an overloaded function is allowed in Parameters<T> Dec 1, 2018
@MartinJohns
Copy link
Contributor

From the documentation for "Conditional Types":

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

https://www.typescriptlang.org/docs/handbook/advanced-types.html
(section "Type inference in conditional types", direct linking unfortunately not possible)

@calvinlauyh
Copy link
Author

Thanks, @MartinJohns
Closing this issue, and also providing the direct link for future reference:
https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-inference-in-conditional-types

@MartinJohns
Copy link
Contributor

Oh, direct linking is possible! I did not bother check the markup, just assumed there would be a visible anchor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants