Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple small issues with 1.9 #2964

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/toolkit/src/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface ActionCreatorWithoutPayload<T extends string = string>
* Calling this {@link redux#ActionCreator} will
* return a {@link PayloadAction} of type `T` with a payload of `undefined`
*/
(): PayloadAction<undefined, T>
(noArgument: void): PayloadAction<undefined, T>
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export interface ApiEndpointQuery<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Definitions extends EndpointDefinitions
> {
name: string
/**
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
*/
Expand All @@ -425,6 +426,7 @@ export interface ApiEndpointMutation<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Definitions extends EndpointDefinitions
> {
name: string
/**
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
*/
Expand Down Expand Up @@ -603,6 +605,7 @@ export const coreModule = (): Module<CoreModule> => ({
safeAssign(
anyApi.endpoints[endpointName],
{
name: endpointName,
select: buildQuerySelector(endpointName, definition),
initiate: buildInitiateQuery(endpointName, definition),
},
Expand All @@ -612,6 +615,7 @@ export const coreModule = (): Module<CoreModule> => ({
safeAssign(
anyApi.endpoints[endpointName],
{
name: endpointName,
select: buildMutationSelector(),
initiate: buildInitiateMutation(endpointName),
},
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
EndpointDefinition,
QueryDefinition,
MutationDefinition,
TagDescription,
} from './endpointDefinitions'
export { fetchBaseQuery } from './fetchBaseQuery'
export type {
Expand Down
7 changes: 7 additions & 0 deletions packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { server } from './mocks/server'
import { rest } from 'msw'
import { SerializeQueryArgs } from '../defaultSerializeQueryArgs'
import { string } from 'yargs'

const originalEnv = process.env.NODE_ENV
beforeAll(() => void ((process.env as any).NODE_ENV = 'development'))
Expand All @@ -43,6 +44,9 @@ test('sensible defaults', () => {
return { url: `user/${id}` }
},
}),
updateUser: build.mutation<unknown, void>({
query: () => '',
}),
}),
})
configureStore({
Expand All @@ -60,6 +64,9 @@ test('sensible defaults', () => {
expectType<TagTypes>(ANY as never)
// @ts-expect-error
expectType<TagTypes>(0)

expect(api.endpoints.getUser.name).toBe('getUser')
expect(api.endpoints.updateUser.name).toBe('updateUser')
})

describe('wrong tagTypes log errors', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react'
import type { Action, AnyAction, ActionCreator } from 'redux'
import type {
PayloadAction,
Expand Down Expand Up @@ -344,3 +345,15 @@ import { expectType } from './helpers'
type AnyPayload = ReturnType<typeof anyCreator>['payload']
expectType<IsAny<AnyPayload, true, false>>(true)
}

// Verify action creators should not be passed directly as arguments
// to React event handlers if there shouldn't be a payload
{
const emptyAction = createAction<void>('empty/action')
function TestComponent() {
// This typically leads to an error like:
// // A non-serializable value was detected in an action, in the path: `payload`.
// @ts-expect-error Should error because `void` and `MouseEvent` aren't compatible
return <button onClick={emptyAction}>+</button>
}
}