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

Mapping generic types breaks inference/narrowing #16267

Closed
jaredru opened this issue Jun 5, 2017 · 2 comments
Closed

Mapping generic types breaks inference/narrowing #16267

jaredru opened this issue Jun 5, 2017 · 2 comments
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue

Comments

@jaredru
Copy link

jaredru commented Jun 5, 2017

TypeScript Version: 2.3.4

Code

interface I {
    x: number | null;
}

function generic<T extends I>(a: T, b: Partial<T>) {
    if (a.x != null) {
        a.x.toFixed(2); // OK
    }

    if (b.x != null) {
        b.x.toFixed(2); // ERROR: Property 'toFixed' does not exist on type 'T["x"]'
    }
}

function notGeneric(a: I, b: Partial<I>) {
    if (a.x != null) {
        a.x.toFixed(2); // OK
    }

    if (b.x != null) {
        b.x.toFixed(2); // OK
    }
}

Expected behavior:

The mapped generic code should compile without error.

Actual behavior:

The mapped generic code does not compile.

For context, the place I'm seeing this issue is React when interacting with props. The React typings for a Component are:

class Component<P, S> {
    props: Readonly<{ children?: ReactNode }> & Readonly<P>;
    // snip
}
@ahejlsberg
Copy link
Member

Duplicate of #14091. Fixed by #15576.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created Fixed A PR has been merged for this issue labels Jun 5, 2017
@mhegazy mhegazy closed this as completed Jun 5, 2017
@jaredru
Copy link
Author

jaredru commented Jun 5, 2017

Ah, I searched but didn't find the dupe. Sorry about that. Thanks for the quick follow up!

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants