Skip to content

Commit f2ecd03

Browse files
committed
Clarify TS generics for queries and mutations
1 parent e45b0f5 commit f2ecd03

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

docs/rtk-query/usage/mutations.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ If the `query` callback needs additional data to generate the URL, it should be
2424

2525
Mutation endpoints may also modify the response contents before the result is cached, define "tags" to identify cache invalidation, and provide cache entry lifecycle callbacks to run additional logic as cache entries are added and removed.
2626

27+
When used with TypeScript, you should supply generics for the return type and the expected query argument: `build.mutation<ReturnType, ArgType>`. If there is no argument, use `void` for the arg type instead.
28+
2729
```ts title="Example of all mutation endpoint options"
2830
// file: types.ts noEmit
2931
export interface Post {
@@ -41,6 +43,7 @@ const api = createApi({
4143
}),
4244
tagTypes: ['Post'],
4345
endpoints: (build) => ({
46+
// The mutation accepts a `Partial<Post>` arg, and returns a `Post`
4447
updatePost: build.mutation<Post, Partial<Post> & Pick<Post, 'id'>>({
4548
// highlight-start
4649
// note: an optional `queryFn` may be used in place of `query`

docs/rtk-query/usage/queries.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ If the `query` callback needs additional data to generate the URL, it should be
3434

3535
Query endpoints may also modify the response contents before the result is cached, define "tags" to identify cache invalidation, and provide cache entry lifecycle callbacks to run additional logic as cache entries are added and removed.
3636

37+
When used with TypeScript, you should supply generics for the return type and the expected query argument: `build.query<ReturnType, ArgType>`. If there is no argument, use `void` for the arg type instead.
38+
3739
```ts title="Example of all query endpoint options"
3840
// file: types.ts noEmit
3941
export interface Post {
@@ -52,8 +54,9 @@ const api = createApi({
5254
}),
5355
tagTypes: ['Post'],
5456
endpoints: (build) => ({
57+
// highlight-start
58+
// The query accepts a number and returns a Post
5559
getPost: build.query<Post, number>({
56-
// highlight-start
5760
// note: an optional `queryFn` may be used in place of `query`
5861
query: (id) => ({ url: `post/${id}` }),
5962
// Pick out data and prevent nested properties in a hook or selector

0 commit comments

Comments
 (0)