Skip to content

Instantiating a mapped type does not update the constraint on the type parameter #27596

@mattmccutchen

Description

@mattmccutchen

I don't expect real users to write code like this example. I just wanted to get this bug out of the way because I think it will make the solution to #27484 easier to understand.

TypeScript Version: master (85a3475)

Search Terms: mapped type parameter constraint instantiate

Code

type MyMap<T> = {[P in keyof T]: T[keyof T]};
function foo<U>(arg: U): MyMap<U> {
    return arg;
}

Expected behavior: No error.

Actual behavior: Error:

Type 'U' is not assignable to type 'MyMap<U>'.
  Type 'U[P]' is not assignable to type 'U[keyof U]'.
    Type 'P' is not assignable to type 'keyof U'.
      ...

This is nonsense because P in keyof T and we set T = U.

Playground Link: link

Related Issues: none found

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions