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

Intersection breaks generic key field inference #26409

Closed
jeremybparagon opened this issue Aug 13, 2018 · 1 comment
Closed

Intersection breaks generic key field inference #26409

jeremybparagon opened this issue Aug 13, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jeremybparagon
Copy link

TypeScript Version: 3.1.0-dev.20180810

Code

const f = <T extends { [K0 in K]: string; } & { cool: string; }, K extends keyof T>(t: T, k: K) =>
{
  const s: string = t[k];
};

Expected behavior: Successful compilation

Actual behavior: I get the following error:

error TS2322: Type 'T[K]' is not assignable to type 'string'.

     const s: string = t[k];
           ~

Playground Link: Link

Additional Notes: This compiles successfully (link):

const f = <T extends { [K0 in K]: string; } & { cool: string; }, K extends keyof T>(t: T, k: K) =>
{
  const s: string = t['cool'];
};

as does this (link):

const f = <T extends { [K0 in K]: string; }, K extends keyof T>(t: T, k: K) =>
{
  const s: string = t[k];
};
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.1 milestone Aug 13, 2018
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Aug 13, 2018
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Aug 27, 2018
@ahejlsberg
Copy link
Member

The issue here is that we're not examining all constraints of indexed access types in type relations, rather we jump straight to base constraints.

In exploring the issue I also discovered that this errors where it shouldn't:

const f = <T extends { [P in K | "cool"]: string; }, K extends keyof T>(t: T, k: K) =>
{
    const s: string = t[k];
    t.cool;  // Error, but shouldn't be
};

I'll put up a PR with a fix shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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