Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Interface and type being treated differently in overload resolution #18779

Closed
Ghabriel opened this issue Sep 26, 2017 · 2 comments
Closed

Interface and type being treated differently in overload resolution #18779

Ghabriel opened this issue Sep 26, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Ghabriel
Copy link

TypeScript Version: 2.5.2

Code

type Map<T> = {[key: string]: T};

type Callback<T> = (key: string, value: T) => void;

declare function foo<T>(obj: Map<T>, callback: Callback<T>): void;
declare function foo<C,T>(obj: {[P in keyof C]: T}, callback: Callback<T>): void;

interface SomeInterface {
    a: number;
    b: number;
}

type SomeType = {
    a: number;
    b: number;
}

let x: SomeInterface = {
    a: 42,
    b: 60
};

let y: SomeType = x;

foo(x, (key, value) => console.log(key, value));
foo(y, (key, value) => console.log(key, value));

Expected behavior:
Both calls to foo should use the first overload (and deduce T as number).

Actual behavior:
The first call to foo uses the second overload and both C and T are deduced as {} (i.e foo<{}, {}>). If the second overload is removed, the program fails to compile:

TS2345: Argument of type 'SomeInterface' is not assignable to parameter of type 'Map<{}>'.
  Index signature is missing in type 'SomeInterface'.

(and other 2 errors since key and value would implicitly become any)

This might be some intended difference between interface and type that I didn't see documented anywhere, but I'm posting this here just to be sure.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 26, 2017

duplicate of #15300

@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 26, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 11, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 11, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants