Skip to content

Inferring function parameters from the return typeΒ #56311

Open
@kyscott18

Description

@kyscott18

πŸ” 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions