1 | import { getDefaultMiddleware } from '@reduxjs/toolkit'
|
2 | import type { Middleware } from 'redux'
|
3 | import type { DispatchForMiddlewares } from '@internal/tsHelpers'
|
4 |
|
5 | declare const expectType: <T>(t: T) => T
|
6 |
|
7 | declare const middleware1: Middleware<{
|
8 | (_: string): number
|
9 | }>
|
10 |
|
11 | declare const middleware2: Middleware<{
|
12 | (_: number): string
|
13 | }>
|
14 |
|
15 | declare const getDispatch: <M extends Array<Middleware>>(
|
16 | m: M
|
17 | ) => DispatchForMiddlewares<M>
|
18 |
|
19 | type ThunkReturn = Promise<'thunk'>
|
20 | declare const thunkCreator: () => () => ThunkReturn
|
21 |
|
22 | {
|
23 | const defaultMiddleware = getDefaultMiddleware()
|
24 |
|
25 |
|
26 | {
|
27 | const concatenated = defaultMiddleware.prepend(middleware1)
|
28 | const dispatch = getDispatch(concatenated)
|
29 | expectType<number>(dispatch('foo'))
|
30 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
31 |
|
32 |
|
33 | expectType<string>(dispatch('foo'))
|
34 | }
|
35 |
|
36 |
|
37 | {
|
38 | const concatenated = defaultMiddleware.prepend(middleware1, middleware2)
|
39 | const dispatch = getDispatch(concatenated)
|
40 | expectType<number>(dispatch('foo'))
|
41 | expectType<string>(dispatch(5))
|
42 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
43 |
|
44 |
|
45 | expectType<string>(dispatch('foo'))
|
46 | }
|
47 |
|
48 |
|
49 | {
|
50 | const concatenated = defaultMiddleware.prepend([
|
51 | middleware1,
|
52 | middleware2,
|
53 | ] as const)
|
54 | const dispatch = getDispatch(concatenated)
|
55 | expectType<number>(dispatch('foo'))
|
56 | expectType<string>(dispatch(5))
|
57 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
58 |
|
59 |
|
60 | expectType<string>(dispatch('foo'))
|
61 | }
|
62 |
|
63 |
|
64 | {
|
65 | const concatenated = defaultMiddleware.concat(middleware1)
|
66 | const dispatch = getDispatch(concatenated)
|
67 | expectType<number>(dispatch('foo'))
|
68 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
69 |
|
70 |
|
71 | expectType<string>(dispatch('foo'))
|
72 | }
|
73 |
|
74 |
|
75 | {
|
76 | const concatenated = defaultMiddleware.concat(middleware1, middleware2)
|
77 | const dispatch = getDispatch(concatenated)
|
78 | expectType<number>(dispatch('foo'))
|
79 | expectType<string>(dispatch(5))
|
80 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
81 |
|
82 |
|
83 | expectType<string>(dispatch('foo'))
|
84 | }
|
85 |
|
86 |
|
87 | {
|
88 | const concatenated = defaultMiddleware.concat([
|
89 | middleware1,
|
90 | middleware2,
|
91 | ] as const)
|
92 | const dispatch = getDispatch(concatenated)
|
93 | expectType<number>(dispatch('foo'))
|
94 | expectType<string>(dispatch(5))
|
95 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
96 |
|
97 |
|
98 | expectType<string>(dispatch('foo'))
|
99 | }
|
100 |
|
101 |
|
102 | {
|
103 | const concatenated = defaultMiddleware
|
104 | .concat(middleware1)
|
105 | .prepend(middleware2)
|
106 | const dispatch = getDispatch(concatenated)
|
107 | expectType<number>(dispatch('foo'))
|
108 | expectType<string>(dispatch(5))
|
109 | expectType<ThunkReturn>(dispatch(thunkCreator()))
|
110 |
|
111 |
|
112 | expectType<string>(dispatch('foo'))
|
113 | }
|
114 | }
|