Skip to content

[Typescript] [Solid Query] typescript inference dependant on order of object keys #8600

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

Closed
vanillacode314 opened this issue Jan 31, 2025 · 1 comment

Comments

@vanillacode314
Copy link

vanillacode314 commented Jan 31, 2025

Describe the bug

Typescript infers type of context as unknown if onMutate comes after onSuccess, onError or onSettled, in the following codeblock mutation1 typescript reports the type of context to be unknown which is wrong, in mutation2 typescript reports type of context to be { foo: string } | undefined which is expected and correct

const mutation1 = createMutation(() => ({
    onError: (error, variables, context) => console.log(context.foo),
    onMutate: ({}) => ({ foo: 'bar' }),
    onSuccess: (data, variables, context) => console.log(context.foo),
}));

const mutation2 = createMutation(() => ({
    onMutate: ({}) => ({ foo: 'bar' }),
    onError: (error, variables, context) => console.log(context.foo),
    onSuccess: (data, variables, context) => console.log(context.foo),
}));

Your minimal, reproducible example

https://stackblitz.com/edit/github-iy58hbhh?file=src%2Froutes%2Findex.tsx

Steps to reproduce

  1. Go to the stackblitz url
  2. Make sure node_modules is installed
  3. Goto src/routes/index.tsx and hover over the context errors

Expected behavior

The order of keys don't matter and type of context is correctly inferred, I understand the workaround is pretty simple but I use a prettier plugin that orders all object keys alphabetically which is good for stable git diffs

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS: Linux vc-arch 6.12.10-zen1-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Sat, 18 Jan 2025 02:26:52 +0000 x86_64 GNU/Linux
Browser: N/A
Editor: Neovim

Tanstack Query adapter

solid-query

TanStack Query version

5.66.0

TypeScript version

5.7.3

Additional context

It only happens if the argument list of onMutate is non empty

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 31, 2025

please, for typescript related issues, typescript playground is so much better.

anyways, the issue is that the producer (onMutate) must come before the consumer (onError, onSuccess). This is a known TS limitation around intra expression inference:

working playground

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants