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

Inferring function parameters from the return type #56311

Open
6 tasks done
kyscott18 opened this issue Nov 4, 2023 · 2 comments
Open
6 tasks done

Inferring function parameters from the return type #56311

kyscott18 opened this issue Nov 4, 2023 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@kyscott18
Copy link

πŸ” Search Terms

"inferring parameters types", "infer input from output types"

βœ… Viability Checklist

⭐ Suggestion

I would like to infer the types of the parameters of a function based on the return type. I have tried a few patterns and I don't think this is currently possible.

πŸ“ƒ Motivating Example

const inferReturnValuesAsKeys = <
  T extends (arg: keyof ReturnType<T> & string) => Record<string, number>,
>(
  t: T,
) => t;

/**
 * @todo arg should be typed as 'a' instead of 'string'
 */
inferReturnValuesAsKeys((arg) => ({ a: 5 }));
// ^?

πŸ’» Use Cases

  1. What do you want to use this for?

Helpful for strict typings and intellisense.

  1. What shortcomings exist with current approaches?

None give a strict enough type.

  1. What workarounds are you using in the meantime?

I don't have the type safety that I want.

@jcalz
Copy link
Contributor

jcalz commented Nov 4, 2023

essentially a duplicate of #49618

@3rd
Copy link

3rd commented Nov 5, 2023

Spent a few hours trying to work around this without success, adding my use case:

// would love for this to work: function create<T>(fn: (root: T) => T): T {}

const example = create((root) => ({
  static: 10,
  computed: () => root.static + 5,
}));

example.computed() // 15

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants