Closed as not planned
Closed as not planned
Description
Bug Report
π Search Terms
overloads, infer
, interfaces, arity
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about overloads
β― Playground Link
Playground link with relevant code
π» Code
type Overloads<T> = T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
}
? [(...args: A1) => R1, (...args: A2) => R2]
: T extends {
(...args: infer A1): infer R1;
}
? [(...args: A1) => R1]
: any;
interface A {
hello(str: string): string
}
type HelloOverloadsOfA = Overloads<A['hello']>; // GOOD [(str: string) => string]
interface B {
hello(): number
hello(str: string): string
}
type HelloOverloadsOfB = Overloads<B['hello']>; // GOOD [() => number, (str: string) => string]
interface C {
hello(): number
}
type HelloOverloadsOfC = Overloads<C['hello']>; // BAD [(...args: unknown[]) => unknown, () => number]
π Actual behavior
When the interface in question contains a single implementation of a function, having arity zero, the Overloads
utility type infers it as two different functions:
(...args: unknown[]) => unknown
, and() => number
π Expected behavior
I expected Typescript to infer it as () => number
alone. This would be consistent with how this utility type infers other methods having a single implementation, but arity > 0.