Skip to content

Difference in behaviour with mapped types between concrete and generic types. #19509

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

Closed
DylanRJohnston opened this issue Oct 26, 2017 · 2 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@DylanRJohnston
Copy link

DylanRJohnston commented Oct 26, 2017

Trying to give better types to the redux library. Came across this issue. The issue appears to be the reindexing via keyof A at the end.

TypeScript Version: 2.7.0-dev.201xxxxx

Code

type Actions = {
  INCREMENT: { x: number }
  DECREMENT: { y: string }
}

type Concrete = {[K in keyof Actions]: {type: K} & Actions[K] }[keyof Actions]

type Generic<A> = {[K in keyof A]: {type: K} & A[K] }[keyof A]

type GenericConcrete = Generic<Actions>

Expected behavior:
Concrete and GenericConcrete have the same type

Actual behavior:
Concrete has the correct type

type Foo = ({
    type: "INCREMENT";
} & {
    x: number;
}) | ({
    type: "DECREMENT";
} & {
    y: string;
})

GenericConcrete has the wrong type

type Foo = ({
    type: "INCREMENT" | "DECREMENT";
} & {
    x: number;
}) | ({
    type: "INCREMENT" | "DECREMENT";
} & {
    y: string;
})

Unless I'm missing some kind of constraint on A in the Generic case that Typescript is able to use in the concrete case to work out the types correctly?

@ahejlsberg
Copy link
Member

I believe this was already fixed by #18042 and I can't reproduce this with typescript@next. Can you verify you're using the latest compiler?

@ahejlsberg ahejlsberg added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Oct 26, 2017
@mhegazy mhegazy added Needs More Info The issue still hasn't been fully clarified and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Oct 26, 2017
@DylanRJohnston
Copy link
Author

Yep, I was on an older version of typescript@next. Sorry for the duplicate ticket.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants