Skip to content

Commit a56a194

Browse files
authored
Merge pull request #2964 from reduxjs/feature/1.9.1-ts-fixes
2 parents 1f78b68 + ce9e05d commit a56a194

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

packages/toolkit/src/createAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export interface ActionCreatorWithoutPayload<T extends string = string>
144144
* Calling this {@link redux#ActionCreator} will
145145
* return a {@link PayloadAction} of type `T` with a payload of `undefined`
146146
*/
147-
(): PayloadAction<undefined, T>
147+
(noArgument: void): PayloadAction<undefined, T>
148148
}
149149

150150
/**

packages/toolkit/src/query/core/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ export interface ApiEndpointQuery<
412412
// eslint-disable-next-line @typescript-eslint/no-unused-vars
413413
Definitions extends EndpointDefinitions
414414
> {
415+
name: string
415416
/**
416417
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
417418
*/
@@ -425,6 +426,7 @@ export interface ApiEndpointMutation<
425426
// eslint-disable-next-line @typescript-eslint/no-unused-vars
426427
Definitions extends EndpointDefinitions
427428
> {
429+
name: string
428430
/**
429431
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
430432
*/
@@ -603,6 +605,7 @@ export const coreModule = (): Module<CoreModule> => ({
603605
safeAssign(
604606
anyApi.endpoints[endpointName],
605607
{
608+
name: endpointName,
606609
select: buildQuerySelector(endpointName, definition),
607610
initiate: buildInitiateQuery(endpointName, definition),
608611
},
@@ -612,6 +615,7 @@ export const coreModule = (): Module<CoreModule> => ({
612615
safeAssign(
613616
anyApi.endpoints[endpointName],
614617
{
618+
name: endpointName,
615619
select: buildMutationSelector(),
616620
initiate: buildInitiateMutation(endpointName),
617621
},

packages/toolkit/src/query/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type {
1010
EndpointDefinition,
1111
QueryDefinition,
1212
MutationDefinition,
13+
TagDescription,
1314
} from './endpointDefinitions'
1415
export { fetchBaseQuery } from './fetchBaseQuery'
1516
export type {

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { server } from './mocks/server'
1919
import { rest } from 'msw'
2020
import { SerializeQueryArgs } from '../defaultSerializeQueryArgs'
21+
import { string } from 'yargs'
2122

2223
const originalEnv = process.env.NODE_ENV
2324
beforeAll(() => void ((process.env as any).NODE_ENV = 'development'))
@@ -43,6 +44,9 @@ test('sensible defaults', () => {
4344
return { url: `user/${id}` }
4445
},
4546
}),
47+
updateUser: build.mutation<unknown, void>({
48+
query: () => '',
49+
}),
4650
}),
4751
})
4852
configureStore({
@@ -60,6 +64,9 @@ test('sensible defaults', () => {
6064
expectType<TagTypes>(ANY as never)
6165
// @ts-expect-error
6266
expectType<TagTypes>(0)
67+
68+
expect(api.endpoints.getUser.name).toBe('getUser')
69+
expect(api.endpoints.updateUser.name).toBe('updateUser')
6370
})
6471

6572
describe('wrong tagTypes log errors', () => {

packages/toolkit/src/tests/createAction.typetest.ts renamed to packages/toolkit/src/tests/createAction.typetest.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react'
12
import type { Action, AnyAction, ActionCreator } from 'redux'
23
import type {
34
PayloadAction,
@@ -344,3 +345,15 @@ import { expectType } from './helpers'
344345
type AnyPayload = ReturnType<typeof anyCreator>['payload']
345346
expectType<IsAny<AnyPayload, true, false>>(true)
346347
}
348+
349+
// Verify action creators should not be passed directly as arguments
350+
// to React event handlers if there shouldn't be a payload
351+
{
352+
const emptyAction = createAction<void>('empty/action')
353+
function TestComponent() {
354+
// This typically leads to an error like:
355+
// // A non-serializable value was detected in an action, in the path: `payload`.
356+
// @ts-expect-error Should error because `void` and `MouseEvent` aren't compatible
357+
return <button onClick={emptyAction}>+</button>
358+
}
359+
}

0 commit comments

Comments
 (0)