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 type] Function type of the returned result is not mapped to the parameter #59124

Closed
inevermore opened this issue Jul 3, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@inevermore
Copy link

inevermore commented Jul 3, 2024

πŸ”Ž Search Terms

"Generic Type","Function","Parameters","ReturnType"

πŸ•— Version & Regression Information

  • This is a bug
  • This changed between versions v5.6.0

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAKhDOwbmgXigbwFBSgQwC5MdcoBjAewBsKAnIxWgSwDsBzAbhIF8vcAjItlJR+dACYR6URq048uvLFkotE5PFSrwo6YRq1xEQkrhZ4AthCIByMpqpHgNgDSmoAMwCuLMkQA8MFAQAB7AECziOgDWECAUHrAISCgAfAAU0UQwAJS6qcQiuLQQwF60LJjc+DpOyJAA2jAAunyk3G64HVhKWKCQUADCDjp6JA0A0lCsULHxif0QCQbazSYi5lZEE2243r5Ei8v2WvCTzQ02+2Q2rQo9XFiSZFR4JZ4+ZMBMFJUnVABlYDMdhQQLBMIRKKzOLLYanFxQABiEPCkR08O0TQuV0+twyJAAjrQQERMWcWpdNhBbp0oAA6RlvNjwIgABTellKUng-iRqSwOSIACVSuUWPUIHyBcpVOoSvAvFRgLoVkCQWx0nYHE5XFAbHgbDVyL9EDkgA

πŸ’» Code

type TestType = {
  a: {
    color: string;
  };
  b: {
    border: string;
  };
};

const calls = {
  callTest: {
    name: 'callTest',
    func: <T extends keyof TestType>(k: T) => {
      return {} as TestType[T];
    },
  },
};

type Calls = {
  [K in keyof typeof calls]: {
    name: K;
    func: typeof calls[K]['func'];
  };
};

declare const callString = <T extends keyof Calls, F extends Calls[T]['func']>(
  qry: Calls[T]['name'],
  ...args: Parameters<F>
): ReturnType<F>

const result = callString('callTest', 'a' as const)

πŸ™ Actual behavior

result type is {color: string} | {border: string}. The type of the returned result is not mapped to the parameter

  • [ ]

πŸ™‚ Expected behavior

result type is {color: string}. The type should map to parameter 'a'

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Jul 3, 2024

I think for this to work TS would need to infer type arguments from indexed access types, but it doesn't. This isn't considered a bug but a missing feature, which is possibly implemented at #20126 and/or #53017, neither of which have been merged.

@inevermore
Copy link
Author

Looking forward the feature

@RyanCavanaugh
Copy link
Member

The feature needed here is the more complex "Given a set of parameter types, produce what an equivalent call expression would have done". ReturnType<F> is definitely not that.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 9, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants