Skip to content

Union types and overloads acts a bit weirdly #33101

Open
@thetutlage

Description

@thetutlage

Acknowledgement

I have asked the question on Gitter and most of the users believe it's a bug and I have search for existing issues and didn't found any.

Issue

Let's start with the code. Playground link

declare class Foo {
	public red(): this
	public red(text: string): string
}

declare class Bar {
	public red(): this
	public red(text: string): string
}

const foo: Foo | Bar = new Foo()
const a = foo.red('text')

In the above code, I expect the variable a to be a string. However, it is string | Bar.

Now, things get even weird when I replace string with object in the return types of red method.

declare class Foo {
	public red(): this
	public red(text: string): object
}

declare class Bar {
	public red(): this
	public red(text: string): object
}

const foo: Foo | Bar = new Foo()
const a = foo.red('text')

Now suddenly, a is an object (which is correct)

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