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

Generic mapping returns union type #36349

Closed
LorenzHenk opened this issue Jan 22, 2020 · 2 comments
Closed

Generic mapping returns union type #36349

LorenzHenk opened this issue Jan 22, 2020 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@LorenzHenk
Copy link

TypeScript Version: 3.8.0-dev.20200119

Search Terms:
generic map
generic function union

Code

export interface Responses {
  ping: {
    pong: number;
  };
  status: {
    status: string;
  };
}

type ResponseTransformerMapping = {
  [Action in keyof Responses]: (data: any) => Responses[Action];
};

export const ResponseTransformers: ResponseTransformerMapping = {
  ping: (data): Responses['ping'] => ({
        pong: data.pong,
      }),
  status: (data): Responses['status']  => ({
        status: data.status,
      }),
};

export const accessData = <Action extends keyof ResponseTransformerMapping>(
    action: Action
): Responses[Action] => {
    return ResponseTransformers[action]({});
};

Expected behavior:

Return type should be Responses[Action]

Actual behavior:

Return type is { pong: number; } | { status: string; }

Type '{ pong: number; } | { status: string; }' is not assignable to type 'Responses[Action]'.
  Type '{ pong: number; }' is not assignable to type 'Responses[Action]'.
    Type '{ pong: number; }' is not assignable to type '{ pong: number; } & { status: string; }'.
      Property 'status' is missing in type '{ pong: number; }' but required in type '{ status: string; }'

Using return ResponseTransformers[action]({}) as Responses[Action]; works fine.

Playground Link: link

Related Issues:
Maybe #33014

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Feb 6, 2020
@RyanCavanaugh
Copy link
Member

TS would need some higher-order reasoning to be able to detect that this code is correct. The problem is that ResponseTransformerMapping doesn't form any concrete connection between the key and the output type of the input key; we'd need to be able to establish that every possible call to ResponseTransformerMapping[key] results in a valid return type.

@LorenzHenk
Copy link
Author

ResponseTransformerMapping doesn't form any concrete connection between the key and the output type of the input key; we'd need to be able to establish that every possible call to ResponseTransformerMapping[key] results in a valid return type

Isn't that enforced by ResponseTransformerMapping + ensuring, that it's called with a valid key by using a generic?

type ResponseTransformerMapping = {
  [Action in keyof Responses]: (data: any) => Responses[Action];
};

// ...

export const accessData = <Action extends keyof ResponseTransformerMapping>( // ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants