question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware in TypeScript

See original GitHub issue

Hi, I’d like to use zustand with TypeScript. Any chance you can write the example middlewares (log and immer) with TypeScript so I can follow and imitate?

I’d really appreciate it. Thank you. @JeremyRH @drcmda.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
coffee-cupcommented, Dec 9, 2019

I have found this middleware type useful

type Middleware<S> = (
  config: StateCreator<S>,
) => (set: SetState<S>, get: GetState<S>, api: StoreApi<S>) => S;

const log: Middleware<StoreType> = config => (set, get, api) =>
  config(args => {
    console.log("  applying", args)
    set(args)
    console.log("  new state", get())
}, get, api);
3reactions
JeremyRHcommented, Nov 15, 2019

Ah sorry, I misunderstood you. The types are exported so you can use them:

import create, { StateCreator, SetState, GetState, StoreApi } from 'zustand'

interface YourState {
  a: number,
  b: string
}

const log = (config: StateCreator<YourState>) => (
  set: SetState<YourState>,
  get: GetState<YourState>,
  api: StoreApi<YourState>
) => config(args => {
  console.log("  applying", args)
  set(args)
  console.log("  new state", get())
}, get, api)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write Express.js middleware with TypeScript
A “middleware” in Express is just a function that intercepts every request so that you can return a custom response or apply a...
Read more >
A generic middleware pattern in Typescript - Evert Pot
Middleware is the actual async/non-async middleware function. I made a type for Next so I don't need to write it out more than...
Read more >
TypeScript Express tutorial #1. Routing, controller, middleware
An example of a middleware is the get callback that handles the HTTP GET request that we've written above. It is a very...
Read more >
Typescript express middleware - node.js - Stack Overflow
I have a simple auth middleware for express. It checks header and if all cool it calls next(). Now when i am in...
Read more >
The Express.js REST API journey continues! Move ... - Toptal
Express.js middleware functions receive request and response objects and a "next" function. They might modify the request or use the response object to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found