Use Draft<S> from immer in TUpdateFunction
See original GitHub issueThis is a great small library I just found, good work : ) There’s a minor issue with TypeScript that would be great if it could be changed:
interface S {
readonly items: readonly string[];
}
const store = new Store<S>({items: []});
// this gives an error "splice does not exist on type ..."
store.update(draft => { draft.items.splice(1, 1) });
// with immer it works
declare const s: S;
produce(s, draft => { draft.items.splice(1, 1) });
This doesn’t transpile as items is a readonly array. immer
has a Draft
type for this case - would be great if it could be used in pullstate:
https://github.com/lostpebble/pullstate/blob/master/src/Store.ts#L26 - https://github.com/lostpebble/pullstate/blob/master/src/Store.ts#L28
import { Draft } from "immer";
export type TUpdateFunction<S> = (draft: Draft<S>, original: S) => void;
type TPathReactionFunction<S> = (paths: TAllPathsParameter<S>, draft: Draft<S>, original: S) => void;
type TReactionFunction<S, T> = (watched: T, draft: Draft<S>, original: S, previousWatched: T) => void;
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Extracting the current state from a draft | Immer - GitHub Pages
Immer exposes a named export current that creates a copy of the current state of the draft. This can be very useful for...
Read more >Use createDraft() inside produce() for better type-safety #393
The ability to use createDraft (or similar) inside produce to import immutable structures into a draft. Motivation. Currently there is no way to ......
Read more >A Better Way to Handle State with Immer | Bits and Pieces
Once all your mutations are completed, Immer will produce the nextState based on the mutations to the draft state. Under the hood, Immer...
Read more >update draft state in promise using immer - Stack Overflow
How to update draft in promise using immer? This is my example code, but it does not work, I get TypeError: Cannot perform...
Read more >Immutability in React with Immer - LogRocket Blog
Learn about immutability in React and how we can use Immer to have an ... Immer will update your state data with the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you very much, that was pretty quick 😃
And yeah, I hadn’t been using readonly either until recently – but I found it’s very useful to keep you (and your co-workers) from accidentally doing something bad : )
Hi @blutorange !
Thanks for the kind words, I’m glad you’re enjoying it 😃
Ah, I see. Makes sense, I’ll look into putting that in now. Never made use of
readonly
before so never ran into this issue- thanks for bringing it to my attention!