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.

[Question] [TypeScript] Best way to decouple set() function

See original GitHub issue

Hello! I wish to be able to dispatch a function instead of using setters and getters. But how is it better to deal with its type?

interface MyStore {
  bears: number;
  honey: number;
  dispatch: (fn: HandlerFn | Partial<MyStore>) => void;
}

// Expected dispatch handler type to be something like:
type HandlerFn = (state: MyStore) => Partial<MyStore>;

export const useStore = create<MyStore>((set) => ({
  bears: 1,
  honey: 3,
  dispatch: (fn: HandlerFn | Partial<MyStore>) => set(fn)
}));

export const incBears = (state: MyStore) => ({
  bears: state.bears + 1,
  honey: state.honey + 3
});

export const incJustHoney = (state: MyStore) => ({
  honey: state.honey + 1
});

export const lotsOfBears = { bears: 999 };

example usage <button onClick={() => dispatch(incBears)}> .

I expect my handler type to be as HandlerFn to return Partial store (or just Partial store in case of an object). However Partial doesn’t match with the set interface (also I tried to compose anything nice out of zustand types)

The error is:

(parameter) fn: HandlerFn
Argument of type 'HandlerFn' is not assignable to parameter of type 'PartialState<MyStore, keyof MyStore, keyof MyStore, keyof MyStore, keyof MyStore>'.
  Type 'HandlerFn' is not assignable to type '(state: MyStore) => MyStore | Pick<MyStore, keyof MyStore>'.
    Type 'Partial<MyStore>' is not assignable to type 'MyStore | Pick<MyStore, keyof MyStore>'.
      Type 'Partial<MyStore>' is not assignable to type 'Pick<MyStore, keyof MyStore>'.
        Types of property 'bears' are incompatible.
          Type 'number | undefined' is not assignable to type 'number'.
            Type 'undefined' is not assignable to type 'number'.ts(2345)

Basically, if I just want to proxy the set function, what would be the best way to annotate handler function?

I think I could have specified the handler types as for example (state: MyStore) => Pick<MyStore, "bears", "honey">, however, I don’t always know which keys my function return

See full example here https://codesandbox.io/s/hopeful-cohen-xy33j?file=/src/store.ts

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Nov 16, 2021

I think I like exactOptionalPropertyTypes. I will try enabling it in zustand/jotai/valtio.

1reaction
devanshjcommented, Nov 16, 2021

Yep it’s already solved with the #662.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ts: set a method in one class equal to a method in another class
Turning the method static is not a very good solution. Now B is coupled to the implementation, not the interface. It means that...
Read more >
Decouple Business Logic using Async Generators - Medium
The only goal of the article is to show how to split the program into smaller independent parts using async generator functions. It...
Read more >
Setting Up Dependency Injection with TypeScript in an Object ...
At the end of this post, you will learn what dependency injection is useful for, how to set it up in your TypeScript...
Read more >
clean-code-typescript - GitHub Pages
Removing duplicate code means creating an abstraction that can handle this set of different things with just one function/module/class.
Read more >
Building Decoupled JavaScript Applications with Postal.js
A great way to solve this problem is through messaging. A great open-source JavaScript messaging library is postal. Take a look at how...
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