Skip to content

Assignability issue in narrowed constrained key type #12635

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

Closed
tinganho opened this issue Dec 3, 2016 · 3 comments
Closed

Assignability issue in narrowed constrained key type #12635

tinganho opened this issue Dec 3, 2016 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@tinganho
Copy link
Contributor

tinganho commented Dec 3, 2016

The below code:

class M {
    __m: any;
}
class C {
    __c: any;
}
type D = typeof M | typeof C;
interface R {
    [i: string]: D;
}
function f1(p: D) {
}
interface P {
    p1: string;
    p2: string;
}
function f2<K extends keyof P>(p: K) {
    let r: R = {};
    if (p in r) {
        f1(r[p]); // Error  `R[K]` is not assignable to `D`
    }
}

Gives me the error R[K] is not assignable to D.

Though isn't R[K] equal to D?

@mhegazy
Copy link
Contributor

mhegazy commented Dec 5, 2016

you do not really need the generic type parameter here. this should be written as:

function f2(p: keyof P) {
    let r: R = {};
    if (p in r) {
        f1(r[p]); 
    }
}

@tinganho
Copy link
Contributor Author

tinganho commented Dec 5, 2016

It seems to work without a generic type parameter. Though, in my actual code I'm using the following signature:

public set<K extends keyof P>(prop: K, value: P[K] | null | undefined): void;

@mhegazy mhegazy added the Bug A bug in TypeScript label Dec 5, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 5, 2016
@ahejlsberg
Copy link
Member

This no longer reproduces in the nightly build. I believe it was fixed in #12770.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jan 3, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants