You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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?
TypeScript Version: 3.8.0-dev.20200119
Search Terms:
generic map
generic function union
Code
Expected behavior:
Return type should be
Responses[Action]
Actual behavior:
Return type is
{ pong: number; } | { status: string; }
Using
return ResponseTransformers[action]({}) as Responses[Action];
works fine.Playground Link: link
Related Issues:
Maybe #33014
The text was updated successfully, but these errors were encountered: