Skip to content

Intersection breaks generic key field inference #26409

Closed
@jeremybparagon

Description

@jeremybparagon

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];
};

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions