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.

Use Draft<S> from immer in TUpdateFunction

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
blutorangecommented, Jun 30, 2020

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 : )

1reaction
lostpebblecommented, Jun 30, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

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