Skip to content

Class method with conditional return type cannot return types common to both conditional branches when the class also has an interfaceΒ #50822

Open
@ascott18

Description

@ascott18

Bug Report

πŸ”Ž Search Terms

method conditional return type not assignable to type

πŸ•— Version & Regression Information

3.3.3+

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export interface Foo<T> {}
export class Foo<T> {
  public bar(): T extends string ? string | number : number {
    return 1;
  }
}

πŸ™ Actual behavior

Return statement is an error, despite being valid for both branches of the conditional return type.

This ONLY happens if the interface Foo exists. If the interface is commented out, the error goes away.

πŸ™‚ Expected behavior

Return statement is not an error, because 1 is assignable to both string | number and number.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions