Created
July 18, 2026 05:06
-
-
Save pfftdammitchris/5b8888846b649635764c47e98c0bd731 to your computer and use it in GitHub Desktop.
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-7.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Store<S> = { | |
| getState: () => S; | |
| dispatch: (action: any) => any; | |
| }; | |
| type ReduxMiddleware<S, A> = ( | |
| store: Store<S> | |
| ) => (next: (action: A) => any) => (action: A) => any; | |
| type ChainMiddleware< | |
| State, | |
| Middlewares extends ReduxMiddleware<any, any>[] | |
| > = Middlewares extends [ | |
| ReduxMiddleware<infer S, infer A>, | |
| ...infer Rest | |
| ] | |
| ? Rest extends ReduxMiddleware<any, any>[] | |
| ? ChainMiddleware<S, Rest> | |
| : S | |
| : State; | |
| function applyMiddleware<S, Middlewares extends ReduxMiddleware<S, any>[]>( | |
| ...middlewares: Middlewares | |
| ) { | |
| return (store: Store<S>) => { | |
| const chain = middlewares.map((middleware) => middleware(store)); | |
| return chain.reduce((composed, middleware) => middleware(composed)); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment