Skip to content

Overload on constants requires cast for resolution #1723

@NoelAbrahams

Description

@NoelAbrahams

Hi,

TS Version: 1.4

In the following the first call to doFoo fails to compile because the overload resolution algorithm picks the general case:

interface Bar {
    bar?: number;
    baz?: string;
}

interface Foo {
    foo(): string;
}

declare function doFoo(kind: 'Foo', bar?: Bar): Foo;
declare function doFoo(kind: string, bar: any): {}

var x = doFoo('Foo', { bar: 1 });
x.foo(); // Error: Property 'foo' does not exist on type '{}'.

var y = doFoo('Foo', <Bar> { bar: 1 });
y.foo(); // Okay

This is a breaking change because it used to work without the need for the cast in 1.3.

I feel this should compile because { bar: 1 } is compatible with the Bar interface.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issueHigh Priority

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions