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 inference error on Union type with keyof #40639

Closed
sidecus opened this issue Sep 18, 2020 · 3 comments
Closed

type inference error on Union type with keyof #40639

sidecus opened this issue Sep 18, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@sidecus
Copy link

sidecus commented Sep 18, 2020

Seeing some type inference error with simple repro code. Not sure it's a bug or not so posting to get some insights.
Kindly note this is not a critical issue, I am just curious about the behavior.

TypeScript Version: 4.0.2 (tried 3.9 as well, same)
Code

class A {
    memberA: string = 'a';
}

class B extends A {
    memberB: string = 'b';
}

class C extends A {
    memberC: string = 'c';
}

class DummyMap<T extends A> {
    constructor(private obj: T) {
        this.obj = obj;
    }
    get<K extends keyof T>(key: K): T[K] {
        return this.obj[key];
    }
}

const map1 = new DummyMap(new B());
const func = (map: DummyMap<B> | DummyMap<C>) => {
    console.log(map.get('memberA')); // compiler error, incompatible signatures
}
func(map1);

//works
const map2: DummyMap<B> | DummyMap<C> = map1;
console.log(map2.get('memberA'));    // ok

Expected behavior:
First map.get should be able to compile.

Actual behavior:
image

Playground Link:
Playground repro

Related Issues:

@MartinJohns
Copy link
Contributor

Duplicate of #7294. Search terms used: union callable

@sidecus
Copy link
Author

sidecus commented Sep 19, 2020

@MartinJohns thanks! Sorry I should have done more searching before opening this. Is this still by design?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 21, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants