MCPcopy
hub / github.com/reduxjs/redux / discriminated

Function discriminated

test/typescript/dispatch.ts:22–46  ·  view source on GitHub ↗

* Dispatch accepts type argument that restricts allowed action types.

()

Source from the content-addressed store, hash-verified

20 * Dispatch accepts type argument that restricts allowed action types.
21 */
22function discriminated() {
23 interface IncrementAction {
24 type: 'INCREMENT'
25 count?: number
26 }
27
28 interface DecrementAction {
29 type: 'DECREMENT'
30 count?: number
31 }
32
33 // Union of all actions in the app.
34 type MyAction = IncrementAction | DecrementAction
35
36 const dispatch: Dispatch<MyAction> = null as any
37
38 dispatch({ type: 'INCREMENT' })
39 dispatch({ type: 'DECREMENT', count: 10 })
40 // Known actions are strictly checked.
41 // @ts-expect-error
42 dispatch({ type: 'DECREMENT', count: '' })
43 // Unknown actions are rejected.
44 // @ts-expect-error
45 dispatch({ type: 'SOME_OTHER_TYPE' })
46}

Callers

nothing calls this directly

Calls 1

dispatchFunction · 0.50

Tested by

no test coverage detected