Skip to content

Commit

Permalink
Merge pull request #2964 from reduxjs/feature/1.9.1-ts-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Nov 30, 2022
2 parents 1f78b68 + ce9e05d commit a56a194
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
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>
}
}

0 comments on commit a56a194

Please sign in to comment.