Skip to content

Interface function of arity of zero gets inferred as having two different method signaturesΒ #53508

Closed as not planned
@steveluscher

Description

@steveluscher

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions