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

Assignment incompatibility with structurally equivalent generic mapped types #12832

Closed
BigDataSamuli opened this issue Dec 11, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@BigDataSamuli
Copy link

TypeScript Version: nightly (2.2.0-dev.20161211)

Code

type Mapped1<T> = {
  [P in keyof T]: T[P];
}

type Mapped2<T> = {
  [P in keyof T]: T[P];
}

function asMapped2<T>(a: Mapped1<T>): Mapped2<T> {
  return a;
}

Expected behavior:
No error.

Actual behavior:

test.ts(10,10): error TS2322: Type 'Mapped1<T>' is not assignable to type 'Mapped2<T>'.
  Type 'T[P]' is not assignable to type 'T[P]'. Two different types with this name exist, but they are 
unrelated.
@ahejlsberg
Copy link
Member

Agreed. I think we need a rule that a mapped type { [P1 in K1] : X1 } is related to a mapped type { [P2 in K2]: X2 } when K1 is related to K2 and X1 is related to an instantiation of X2 that substitutes P1 for P2.

@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 4, 2017
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Jan 4, 2017
@BigDataSamuli
Copy link
Author

FWIW, my original use case was similar to the following:

interface Fooify<T> {
  foo: T;
}

interface Barify<T> {
  bar: T;
}

type Mapped1<T> = {
  [P in keyof T]: Fooify<T[P]>;
}

type Mapped2<T> = {
  [P in keyof T]: Barify<T[P]> | Fooify<T[P]>;
}

function asMapped2<T>(a: Mapped1<T>): Mapped2<T> {
  return a;
}

which errors with

test.ts(18,10): error TS2322: Type 'Mapped1<T>' is not assignable to type 'Mapped2<T>'.
  Type 'Fooify<T[P]>' is not assignable to type 'Barify<T[P]> | Fooify<T[P]>'.
    Type 'Fooify<T[P]>' is not assignable to type 'Fooify<T[P]>'. Two different types with this name exist, but they are unrelated.
      Type 'T[P]' is not assignable to type 'T[P]'. Two different types with this name exist, but they are unrelated.

I presumed the issue in the OP is the underlying cause for this.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jan 12, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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