Skip to content

Using generic argument that extends something, stops inferring super keys when Exclude is used in addition to keyof and Pick #27928

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

Open
sceutre opened this issue Oct 16, 2018 · 2 comments
Labels
Domain: Conditional Types The issue relates to conditional types Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@sceutre
Copy link

sceutre commented Oct 16, 2018

TypeScript Version: 3.1.0

Search Terms:
keyof extends pick exclude

Code

interface Base {
    base1: number;
    base2: number;
}

class Data<T extends Base> {
   alpha(k:keyof Pick<T, Exclude<keyof T, "base1">>) {   }
    beta(k:keyof Pick<T, keyof T>) {   }
}

function wrapped<T extends Base>() {
    let d = new Data<T>();
    d.alpha("base2"); // Argument of type '"base2"' is not assignable to parameter of type 'Exclude<keyof T, "base1">'. 
    d.beta("base2");  // works
}

Expected behavior:

Both alpha and beta should work.

Actual behavior:

Alpha does not work: ` Argument of type '"base2"' is not assignable to parameter of type 'Exclude<keyof T, "base1">'.

Playground Link:

Playground link

Related Issues:

@weswigham weswigham added Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types labels Oct 16, 2018
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed Bug A bug in TypeScript labels Oct 25, 2018
@DanielRosenwasser
Copy link
Member

Any thoughts on this one @weswigham @ahejlsberg?

@apexskier
Copy link

I'm not sure, but I think I've found the same issue in v3.3.1. Please let me know if I'm wrong and I can open a new issue. This is a minimal reproduction, my real use case has the Exclude<Exclude type generated from nested react higher order components.

playground

interface Base {
    a: number;
    b: number;
    c: number;
    d: number;
    e: number;
    f: number;
}

type BaseA = Exclude<keyof Base, "a" | "c" | "b" | "d">;
type BaseB = Exclude<Exclude<keyof Base, "a" | "b">, "c" | "d">;

function testingBase(a: BaseA): BaseB {
    return a; // works, as expected
}

type A<T> = Exclude<keyof T, "a" | "c" | "b" | "d">;
type B<T> = Exclude<Exclude<keyof T, "a" | "b">, "c" | "d">;

function testing<T>(a: A<T>): B<T> {
    return a; // fails unexpectedly with "Type 'Exclude<keyof T, "a" | "b" | "c" | "d">' is not assignable to type 'Exclude<Exclude<keyof T, "a" | "b">, "c" | "d">'."
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Conditional Types The issue relates to conditional types Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants