Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Type Merging between extends and intersection #8606

Open
kitsonk opened this issue May 14, 2016 · 5 comments
Open

Type Merging between extends and intersection #8606

kitsonk opened this issue May 14, 2016 · 5 comments
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@kitsonk
Copy link
Contributor

kitsonk commented May 14, 2016

TypeScript Version:

1.8.10

Code

interface Foo {
    on(type: 'foo', listener: (event: MouseEvent) => boolean): boolean;
    on(type: string, listener: (event: Event) => boolean): boolean;
}

interface Bar {
    on(type: 'bar', listener: (event: MSGestureEvent) => boolean): boolean;
    on(type: string, listener: (event: Event) => boolean): boolean;
}

interface FooBarA extends Foo, Bar {} // Error: not correctly extended

interface FooBarB extends Foo, Bar {
    on(type: 'foo', listener: (event: MouseEvent) => boolean): boolean;
    on(type: 'bar', listener: (event: MSGestureEvent) => boolean): boolean;
    on(type: string, listener: (event: Event) => boolean): boolean;
}

type FooBarC = Foo & Bar;

let foobar: FooBarC;

foobar.on('foo', (event) => { return false; });

Expected behavior:

That FooBarA would work instead of throwing an error.

Actual behavior:

FooBarA complains Interface 'FooBarA' cannot simultaneously extend types 'Foo' and 'Bar'. Named property 'on' of types 'Foo' and 'Bar' are not identical.

While that is true in the strictest sense, the "base" override matches, which means in theory the string literal types could be merged and the error should only occur if there is a conflict between a specific string literal type.

Using the intersection type works perfectly fine and the resulting type mirrors the runtime behaviour. Using the "reimplement all methods" (FooBarB) gets really tedious really quickly when you are trying to do something like model event listeners like the above.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels May 16, 2016
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels May 16, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone May 16, 2016
@RyanCavanaugh RyanCavanaugh added the Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". label May 16, 2016
@RyanCavanaugh
Copy link
Member

Proposed fix: When we consider signatures, after flattening the list of all signatures, we should then stable-sort, placing signatures with the more string literal types before those with fewer.

@kitsonk
Copy link
Contributor Author

kitsonk commented Nov 4, 2016

I am going to try to tackle this.

@rjamesnw
Copy link

rjamesnw commented May 24, 2017

I just ran into something similar also.

class A {
    private value?: any;
}

interface IA extends A { } 

class B {
    private value?: any;
}

interface IB extends B, IA { } 

(https://goo.gl/Yo4LJA)

Interface 'IB' cannot simultaneously extend types 'B' and 'IA'.
Named property 'value' of types 'B' and 'IA' are not identical.

Actually, I was surprised it even cares to pull in private members to a public interface.

@Kingwl
Copy link
Contributor

Kingwl commented Dec 6, 2017

ping @RyanCavanaugh
i don't understand why sort the signatures can solve this issue....
Can you explain it for me? 😄

@HolgerJeromin
Copy link
Contributor

My usecase was this:

class MyClass { };
export interface IOptionsBase {
}
export interface IOptions extends IOptionsBase {
    myCb?: (this: Window, data: boolean) => void;
}
export interface IOptionsEx extends IOptionsBase {
    myCb?: (this: MyClass, data: number) => void;
}

// should get:       myCb?: (this: Window|MyClass, data: number) => void;
export interface ICtrlMetaData extends IOptions, IOptionsEx {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants