Skip to content

Commit df00e1e

Browse files
committed
showcase potential circular reference
1 parent 4e5f59d commit df00e1e

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

type-tests/files/createSlice.typetest.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
SerializedError,
1616
PayloadActionCreator,
1717
AsyncThunk,
18-
CaseReducer
18+
CaseReducer,
19+
configureStore
1920
} from '../../src'
2021

2122
function expectType<T>(t: T) {
@@ -515,10 +516,16 @@ const value = actionCreators.anyKey
515516
TestArg,
516517
TestReturned,
517518
{
518-
rejectValue: TestReject
519+
rejectValue: TestReject // this introduces a circular reference
520+
dispatch: StoreDispatch // this introduces a circular reference
521+
state: StoreState
519522
}
520523
>(
521524
function payloadCreator(arg, api) {
525+
// here would be a circular reference
526+
expectType<StoreState>(api.getState())
527+
// here would be a circular reference
528+
expectType<StoreDispatch>(api.dispatch)
522529
expectType<TestArg>(arg)
523530
expectType<(value: TestReject) => any>(api.rejectWithValue)
524531
return Promise.resolve({ payload: 'foo' })
@@ -544,6 +551,15 @@ const value = actionCreators.anyKey
544551
})
545552
})
546553

554+
// this would cause a circular reference:
555+
// const store = configureStore({ reducer: { test: slice.reducer } })
556+
// this is the only way to break it (could be a cast at the export from the slice file)
557+
const testReducer = slice.reducer as Reducer<TestState>
558+
const store = configureStore({ reducer: { test: testReducer } })
559+
560+
type StoreState = ReturnType<typeof store.getState>
561+
type StoreDispatch = typeof store.dispatch
562+
547563
expectType<PayloadActionCreator<string>>(slice.actions.normalReducer)
548564
expectType<AsyncThunk<TestReturned, TestArg, {}>>(slice.actions.testInfer)
549565
expectType<AsyncThunk<TestReturned, TestArg, { rejectValue: TestReject }>>(

0 commit comments

Comments
 (0)