Skip to content

Commit 47e6200

Browse files
committed
Merge branch 'master' of https://github.com/julian-ford/redux-toolkit into pr/add-listenerapi-cancel
2 parents 266f1ed + 2fe9e73 commit 47e6200

File tree

5 files changed

+61
-26
lines changed

5 files changed

+61
-26
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
fail-fast: false
106106
matrix:
107107
node: ['16.x']
108-
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9.2-rc']
108+
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9.5', '5.0', '5.1', '5.2']
109109
steps:
110110
- name: Checkout repo
111111
uses: actions/checkout@v2

packages/toolkit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reduxjs/toolkit",
3-
"version": "1.9.6",
3+
"version": "1.9.7",
44
"description": "The official, opinionated, batteries-included toolset for efficient Redux development",
55
"author": "Mark Erikson <mark@isquaredsoftware.com>",
66
"license": "MIT",
@@ -28,6 +28,7 @@
2828
"types": "dist/index.d.ts",
2929
"devDependencies": {
3030
"@microsoft/api-extractor": "^7.13.2",
31+
"@phryneas/ts-version": "^1.0.2",
3132
"@size-limit/preset-small-lib": "^4.11.0",
3233
"@testing-library/react": "^13.3.0",
3334
"@testing-library/user-event": "^13.1.5",

packages/toolkit/src/query/react/namedHooks.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,37 @@ import type {
66
QueryDefinition,
77
} from '@reduxjs/toolkit/query'
88

9+
type QueryHookNames<Definitions extends EndpointDefinitions> = {
10+
[K in keyof Definitions as Definitions[K] extends {
11+
type: DefinitionType.query
12+
}
13+
? `use${Capitalize<K & string>}Query`
14+
: never]: UseQuery<
15+
Extract<Definitions[K], QueryDefinition<any, any, any, any>>
16+
>
17+
}
18+
19+
type LazyQueryHookNames<Definitions extends EndpointDefinitions> = {
20+
[K in keyof Definitions as Definitions[K] extends {
21+
type: DefinitionType.query
22+
}
23+
? `useLazy${Capitalize<K & string>}Query`
24+
: never]: UseLazyQuery<
25+
Extract<Definitions[K], QueryDefinition<any, any, any, any>>
26+
>
27+
}
28+
29+
type MutationHookNames<Definitions extends EndpointDefinitions> = {
30+
[K in keyof Definitions as Definitions[K] extends {
31+
type: DefinitionType.mutation
32+
}
33+
? `use${Capitalize<K & string>}Mutation`
34+
: never]: UseMutation<
35+
Extract<Definitions[K], MutationDefinition<any, any, any, any>>
36+
>
37+
}
38+
939
export type HooksWithUniqueNames<Definitions extends EndpointDefinitions> =
10-
keyof Definitions extends infer Keys
11-
? Keys extends string
12-
? Definitions[Keys] extends { type: DefinitionType.query }
13-
? {
14-
[K in Keys as `use${Capitalize<K>}Query`]: UseQuery<
15-
Extract<Definitions[K], QueryDefinition<any, any, any, any>>
16-
>
17-
} &
18-
{
19-
[K in Keys as `useLazy${Capitalize<K>}Query`]: UseLazyQuery<
20-
Extract<Definitions[K], QueryDefinition<any, any, any, any>>
21-
>
22-
}
23-
: Definitions[Keys] extends { type: DefinitionType.mutation }
24-
? {
25-
[K in Keys as `use${Capitalize<K>}Mutation`]: UseMutation<
26-
Extract<Definitions[K], MutationDefinition<any, any, any, any>>
27-
>
28-
}
29-
: never
30-
: never
31-
: never
40+
QueryHookNames<Definitions> &
41+
LazyQueryHookNames<Definitions> &
42+
MutationHookNames<Definitions>

packages/toolkit/src/tests/createAsyncThunk.typetest.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
AsyncThunkFulfilledActionCreator,
1818
AsyncThunkRejectedActionCreator,
1919
} from '@internal/createAsyncThunk'
20+
import type { TSVersion } from '@phryneas/ts-version'
2021

2122
const ANY = {} as any
2223
const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
@@ -287,8 +288,22 @@ const anyAction = { type: 'foo' } as AnyAction
287288
// in that case, we have to forbid this behaviour or it will make arguments optional everywhere
288289
{
289290
const asyncThunk = createAsyncThunk('test', (arg?: number) => 0)
290-
expectType<(arg?: number) => any>(asyncThunk)
291-
asyncThunk()
291+
292+
// Per https://github.com/reduxjs/redux-toolkit/issues/3758#issuecomment-1742152774 , this is a bug in
293+
// TS 5.1 and 5.2, that is fixed in 5.3. Conditionally run the TS assertion here.
294+
type IsTS51Or52 = TSVersion.Major extends 5
295+
? TSVersion.Minor extends 1 | 2
296+
? true
297+
: false
298+
: false
299+
300+
type expectedType = IsTS51Or52 extends true
301+
? (arg: number) => any
302+
: (arg?: number) => any
303+
expectType<expectedType>(asyncThunk)
304+
// We _should_ be able to call this with no arguments, but we run into that error in 5.1 and 5.2.
305+
// Disabling this for now.
306+
// asyncThunk()
292307
asyncThunk(5)
293308
// @ts-expect-error
294309
asyncThunk('string')

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6421,6 +6421,13 @@ __metadata:
64216421
languageName: node
64226422
linkType: hard
64236423

6424+
"@phryneas/ts-version@npm:^1.0.2":
6425+
version: 1.0.2
6426+
resolution: "@phryneas/ts-version@npm:1.0.2"
6427+
checksum: d51914a8ea35ff8b686a9379b9e9fe6d5b5feaf2e7511b880d2835015736e33bc82952bbc369918f251d4a755f32f4a9c4a34b0ec4dfdbc3e87a41d26401105c
6428+
languageName: node
6429+
linkType: hard
6430+
64246431
"@pmmmwh/react-refresh-webpack-plugin@npm:^0.5.3":
64256432
version: 0.5.7
64266433
resolution: "@pmmmwh/react-refresh-webpack-plugin@npm:0.5.7"
@@ -6554,6 +6561,7 @@ __metadata:
65546561
resolution: "@reduxjs/toolkit@workspace:packages/toolkit"
65556562
dependencies:
65566563
"@microsoft/api-extractor": ^7.13.2
6564+
"@phryneas/ts-version": ^1.0.2
65576565
"@size-limit/preset-small-lib": ^4.11.0
65586566
"@testing-library/react": ^13.3.0
65596567
"@testing-library/user-event": ^13.1.5

0 commit comments

Comments
 (0)