* Dispatch accepts type argument that restricts allowed action types.
()
| 20 | * Dispatch accepts type argument that restricts allowed action types. |
| 21 | */ |
| 22 | function 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 | } |
nothing calls this directly
no test coverage detected