Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
Browse files Browse the repository at this point in the history
…o improve-treeshakeability
  • Loading branch information
aryaemami59 committed Jul 25, 2024
2 parents a3874b8 + ae1dcbf commit 15c0f92
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 42 deletions.
5 changes: 4 additions & 1 deletion docs/api/createListenerMiddleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,14 @@ To fix this, the middleware provides types for defining "pre-typed" versions of
import { createListenerMiddleware, addListener } from '@reduxjs/toolkit'
import type { RootState, AppDispatch } from './store'

declare type ExtraArgument = { foo: string }

export const listenerMiddleware = createListenerMiddleware()

export const startAppListening = listenerMiddleware.startListening.withTypes<
RootState,
AppDispatch
AppDispatch,
ExtraArgument
>()

export const addAppListener = addListener.withTypes<RootState, AppDispatch>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type AppThunk<ThunkReturnType = void> = ThunkAction<
unknown,
Action
>
type ExtraArgument = { foo: string }

describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
const listenerMiddleware = createListenerMiddleware()
Expand All @@ -77,11 +78,12 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
test('startListening.withTypes', () => {
const startAppListening = listenerMiddleware.startListening.withTypes<
RootState,
AppDispatch
AppDispatch,
ExtraArgument
>()

expectTypeOf(startAppListening).toEqualTypeOf<
TypedStartListening<RootState, AppDispatch>
TypedStartListening<RootState, AppDispatch, ExtraArgument>
>()

startAppListening({
Expand All @@ -102,6 +104,8 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {

expectTypeOf(stateCurrent).toEqualTypeOf<RootState>()

expectTypeOf(listenerApi.extra).toEqualTypeOf<ExtraArgument>()

timeout = 1
takeResult = await listenerApi.take(increment.match, timeout)

Expand All @@ -111,10 +115,14 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
})

test('addListener.withTypes', () => {
const addAppListener = addListener.withTypes<RootState, AppDispatch>()
const addAppListener = addListener.withTypes<
RootState,
AppDispatch,
ExtraArgument
>()

expectTypeOf(addAppListener).toEqualTypeOf<
TypedAddListener<RootState, AppDispatch>
TypedAddListener<RootState, AppDispatch, ExtraArgument>
>()

store.dispatch(
Expand All @@ -126,27 +134,34 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
expectTypeOf(state).toEqualTypeOf<RootState>()

expectTypeOf(listenerApi.dispatch).toEqualTypeOf<AppDispatch>()

expectTypeOf(listenerApi.extra).toEqualTypeOf<ExtraArgument>()
},
}),
)
})

test('removeListener.withTypes', () => {
const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
const removeAppListener = removeListener.withTypes<
RootState,
AppDispatch,
ExtraArgument
>()

expectTypeOf(removeAppListener).toEqualTypeOf<
TypedRemoveListener<RootState, AppDispatch>
TypedRemoveListener<RootState, AppDispatch, ExtraArgument>
>()
})

test('stopListening.withTypes', () => {
const stopAppListening = listenerMiddleware.stopListening.withTypes<
RootState,
AppDispatch
AppDispatch,
ExtraArgument
>()

expectTypeOf(stopAppListening).toEqualTypeOf<
TypedStopListening<RootState, AppDispatch>
TypedStopListening<RootState, AppDispatch, ExtraArgument>
>()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,33 @@ type AppThunk<ThunkReturnType = void> = ThunkAction<
Action
>

type ExtraArgument = { foo: string }

const listenerMiddleware = createListenerMiddleware()

const startAppListening = listenerMiddleware.startListening.withTypes<
RootState,
AppDispatch
AppDispatch,
ExtraArgument
>()

const stopAppListening = listenerMiddleware.stopListening.withTypes<
RootState,
AppDispatch
AppDispatch,
ExtraArgument
>()

const addAppListener = addListener.withTypes<RootState, AppDispatch>()
const addAppListener = addListener.withTypes<
RootState,
AppDispatch,
ExtraArgument
>()

const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
const removeAppListener = removeListener.withTypes<
RootState,
AppDispatch,
ExtraArgument
>()

describe('startAppListening.withTypes', () => {
test('should return startListening', () => {
Expand Down
Loading

0 comments on commit 15c0f92

Please sign in to comment.