Skip to content

Inference is broken for object generics with return function #46977

Closed

Description

Bug Report

🔎 Search Terms

inference, generics, return type

🕗 Version & Regression Information

I'm not sure when this bug "appeared", but it seems to behave this way for every version, even the nightly

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about object property inference with regards to function returns

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.5.2#code/GYVwdgxgLglg9mABCAzgUwIojQJwJ4A8AKlrngNJp4A0iRAIgIZSMB8AFAPpwAOsCKAFyIA3gChEiAI7Z8lPMJKyKVANwTpygGJhh7CAihoAHlAD8wkZrLzFpOVUQBfAJSIAvKzpMW6yQgBlEAgINBQhRHYAE2ZGRR9GN09EADc4GCj1V1EnMTEDMBQoawc8D0QAbQAiIyKq2gBGWgAmWisAZmEoHGxnAF08gHpBxDA4RAKjU1HxiHwxVExldnFJGRsqag11-B09F2EwEABbACNcDy8mjUDg0PC9GJYkryfGADooOC0YYzQo9guLaudRiYaIRgoRAoOAICFQgCSyHQEygxlob0QMChY0QABsEABzC5wYCIKB4HhoUYnc44RDAOD024hMIoBboex4FbbZTyLZrbS6SLQYwHGlnC7Ja6ScEAdQAFmVsYgAAZvVWIUnkynU1WMMB4VVmG5gIKsh6RN4vRBvT7fX7-QHAlzqIA

💻 Code

function useQuery<TQueryKey, TData>(_options: {
  queryKey: TQueryKey;
  queryFn: (context?: { queryKey: TQueryKey }) => TData;
  onSuccess: (data: TData) => void;
}) {}

const queryKey = ["test", 1, 2, { 3: true }]

// no context no cry
useQuery({
  queryKey,
  queryFn: (): number => 1,
  onSuccess: (data) => data.toFixed(),
});

// as soon as I use ctx, data is no longer of type number for onSuccess
useQuery({
  queryKey,
  queryFn: (ctx): number => 1,
  // Why is `data` of type `any`?
  onSuccess: (data) => data.toFixed(),
});

🙁 Actual behavior

In the second example the data argument on the onSuccess function is typed as any.

🙂 Expected behavior

In the second example the data argument should be typed as the return value of the queryFn property, or more specifically, number

Activity

  1. tannerlinsley commented on Dec 1, 2021

    @tannerlinsley
    Author
  2. MartinJohns commented on Dec 1, 2021

    @MartinJohns
    Contributor

    Pretty sure it's the same as #43371.

  3. tannerlinsley commented on Dec 1, 2021

    @tannerlinsley
    Author

    Thanks Martin Johns (@MartinJohns). I'll track it there.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions