Skip to content

Commit c2861c4

Browse files
authored
fix(types): make TVariables default to unknown on Mutation (#7433)
* fix: make TVariables default to unknown on Mutation we only need `void` as the default for MutationOptions so that we can fire mutations with no parameters without passing undefined to them explicitly. For reading mutations and mutationState, unknown is better. * refactor: remove unnecessary type assertion * refactor: remove unnecessary type assertion * refactor: some more remove unnecessary type assertion
1 parent 3a9e046 commit c2861c4

File tree

7 files changed

+13
-42
lines changed

7 files changed

+13
-42
lines changed

packages/angular-query-experimental/src/inject-mutation-state.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
untracked,
88
} from '@angular/core'
99
import {
10-
type DefaultError,
1110
type Mutation,
1211
type MutationCache,
1312
type MutationFilters,
@@ -22,9 +21,7 @@ import type { Injector, Signal } from '@angular/core'
2221

2322
type MutationStateOptions<TResult = MutationState> = {
2423
filters?: MutationFilters
25-
select?: (
26-
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
27-
) => TResult
24+
select?: (mutation: Mutation) => TResult
2825
}
2926

3027
function getResult<TResult = MutationState>(
@@ -35,11 +32,7 @@ function getResult<TResult = MutationState>(
3532
.findAll(options.filters)
3633
.map(
3734
(mutation): TResult =>
38-
(options.select
39-
? options.select(
40-
mutation as Mutation<unknown, DefaultError, unknown, unknown>,
41-
)
42-
: mutation.state) as TResult,
35+
(options.select ? options.select(mutation) : mutation.state) as TResult,
4336
)
4437
}
4538

packages/query-core/src/hydration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
DefaultError,
23
MutationKey,
34
MutationMeta,
45
MutationOptions,
@@ -21,7 +22,7 @@ export interface DehydrateOptions {
2122
export interface HydrateOptions {
2223
defaultOptions?: {
2324
queries?: QueryOptions
24-
mutations?: MutationOptions
25+
mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>
2526
}
2627
}
2728

packages/query-core/src/mutation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface MutationConfig<TData, TError, TVariables, TContext> {
2323
export interface MutationState<
2424
TData = unknown,
2525
TError = DefaultError,
26-
TVariables = void,
26+
TVariables = unknown,
2727
TContext = unknown,
2828
> {
2929
context: TContext | undefined
@@ -81,7 +81,7 @@ export type Action<TData, TError, TVariables, TContext> =
8181
export class Mutation<
8282
TData = unknown,
8383
TError = DefaultError,
84-
TVariables = void,
84+
TVariables = unknown,
8585
TContext = unknown,
8686
> extends Removable {
8787
state: MutationState<TData, TError, TVariables, TContext>

packages/react-query/src/useMutationState.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as React from 'react'
44
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
55
import { useQueryClient } from './QueryClientProvider'
66
import type {
7-
DefaultError,
87
Mutation,
98
MutationCache,
109
MutationFilters,
@@ -25,9 +24,7 @@ export function useIsMutating(
2524

2625
type MutationStateOptions<TResult = MutationState> = {
2726
filters?: MutationFilters
28-
select?: (
29-
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
30-
) => TResult
27+
select?: (mutation: Mutation) => TResult
3128
}
3229

3330
function getResult<TResult = MutationState>(
@@ -38,11 +35,7 @@ function getResult<TResult = MutationState>(
3835
.findAll(options.filters)
3936
.map(
4037
(mutation): TResult =>
41-
(options.select
42-
? options.select(
43-
mutation as Mutation<unknown, DefaultError, unknown, unknown>,
44-
)
45-
: mutation.state) as TResult,
38+
(options.select ? options.select(mutation) : mutation.state) as TResult,
4639
)
4740
}
4841

packages/solid-query/src/__tests__/useMutationState.test-d.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ describe('useMutationState', () => {
88
filters: { status: 'pending' },
99
}))
1010

11-
expectTypeOf(result()).toEqualTypeOf<
12-
Array<MutationState<unknown, Error, void, unknown>>
13-
>()
11+
expectTypeOf(result()).toEqualTypeOf<Array<MutationState>>()
1412
})
1513
it('should infer with select', () => {
1614
const result = useMutationState(() => ({

packages/solid-query/src/useMutationState.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createEffect, createMemo, createSignal, onCleanup } from 'solid-js'
22
import { replaceEqualDeep } from '@tanstack/query-core'
33
import { useQueryClient } from './QueryClientProvider'
44
import type {
5-
DefaultError,
65
Mutation,
76
MutationCache,
87
MutationFilters,
@@ -13,9 +12,7 @@ import type { QueryClient } from './QueryClient'
1312

1413
type MutationStateOptions<TResult = MutationState> = {
1514
filters?: MutationFilters
16-
select?: (
17-
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
18-
) => TResult
15+
select?: (mutation: Mutation) => TResult
1916
}
2017

2118
function getResult<TResult = MutationState>(
@@ -26,11 +23,7 @@ function getResult<TResult = MutationState>(
2623
.findAll(options.filters)
2724
.map(
2825
(mutation): TResult =>
29-
(options.select
30-
? options.select(
31-
mutation as Mutation<unknown, DefaultError, unknown, unknown>,
32-
)
33-
: mutation.state) as TResult,
26+
(options.select ? options.select(mutation) : mutation.state) as TResult,
3427
)
3528
}
3629

packages/vue-query/src/useMutationState.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { useQueryClient } from './useQueryClient'
1010
import { cloneDeepUnref } from './utils'
1111
import type { DeepReadonly, Ref } from 'vue-demi'
1212
import type {
13-
DefaultError,
1413
MutationFilters as MF,
1514
Mutation,
1615
MutationState,
@@ -51,9 +50,7 @@ export function useIsMutating(
5150

5251
export type MutationStateOptions<TResult = MutationState> = {
5352
filters?: MutationFilters
54-
select?: (
55-
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
56-
) => TResult
53+
select?: (mutation: Mutation) => TResult
5754
}
5855

5956
function getResult<TResult = MutationState>(
@@ -64,11 +61,7 @@ function getResult<TResult = MutationState>(
6461
.findAll(options.filters)
6562
.map(
6663
(mutation): TResult =>
67-
(options.select
68-
? options.select(
69-
mutation as Mutation<unknown, DefaultError, unknown, unknown>,
70-
)
71-
: mutation.state) as TResult,
64+
(options.select ? options.select(mutation) : mutation.state) as TResult,
7265
)
7366
}
7467

0 commit comments

Comments
 (0)