refactor(query-core): change QueryMeta and MutationMeta to accept generic types#5412
refactor(query-core): change QueryMeta and MutationMeta to accept generic types#5412vojvodics wants to merge 2 commits intoTanStack:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
|
please have a look at how we solved this differently in v5: https://tanstack.com/query/v5/docs/react/typescript#registering-a-global-error I realise the docs are for error only, but we have the same thing for meta: query/packages/query-core/src/types.ts Lines 11 to 15 in b657107 |
|
Interesting... Although I still don't see how that implementation will help me infer types in meta functions 😕 In v5 I'd override it like this: But I still won't be able to infer the values, so I think the Register needs to be generic (for this to work): I can create a PR for the v5 branch if you'd like this to be a v5 feature only I can also create a reproduction repo if it helps to highlight the problem this solves |
|
here, but I can see that the function use-case is pretty interesting. If you can make a PR for v5 (alpha branch), I can take another look! |
|
i try to solve it using declaration merging but this doesn't seem to work declare module '@tanstack/react-query' {
interface MutationMeta {
customField1: string;
customField2: number;
}
}
// mutation.meta?.customField1 <- doesn't type safeupdates: nevermind, it does work |
Referring to #4253 and https://tkdodo.eu/blog/breaking-react-querys-api-on-purpose.
Assuming I have a setup like this:
This PR enables you to infer the types correctly in meta field:
Besides inference this will catch type errors when refactoring - if we want to refactor deleteTodo from
function deleteTodo(todo: Todo): Promise<void>tofunction deleteTodo(options: {todo: Todo, projectId: string}): Promise<void>, we'll get a type error, however, without inference we could easily miss this.Is there any way to ignore the errors of unused types?
Is there a docs section that I should update?