Skip to content

Generic lookup on mapped types cannot resolve type #29528

@lingz

Description

@lingz

TypeScript Version: 3.2.0

Search Terms: Mapped Types, Generics

Code

type letters = 'a' | 'b'

const identityMapping: {
  [letter in letters]: letter
} = {
  'a': 'a',
  'b': 'b'
};

function getIdentity<T extends letters>(t: T): T {
  return identityMapping[t];
}

Expected behavior:
Compiles without type errors

Actual behavior:

[ts]
Type '{ a: "a"; b: "b"; }[T]' is not assignable to type 'T'.
  Type 'letters' is not assignable to type 'T'.
    Type '"a"' is not assignable to type 'T'. [2322]

Playground Link:
http://www.typescriptlang.org/play/#src=type%20letters%20%3D%20'a'%20%7C%20'b'%0D%0A%0D%0Aconst%20identityMapping%3A%20%7B%0D%0A%20%20%5Bletter%20in%20letters%5D%3A%20letter%0D%0A%7D%20%3D%20%7B%0D%0A%20%20'a'%3A%20'a'%2C%0D%0A%20%20'b'%3A%20'b'%0D%0A%7D%3B%0D%0A%0D%0Afunction%20getIdentity%3CT%20extends%20letters%3E(t%3A%20T)%3A%20T%20%7B%0D%0A%20%20return%20identityMapping%5Bt%5D%3B%0D%0A%7D%0D%0A

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions