Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Draft<S> from immer in TUpdateFunction #48

Closed
blutorange opened this issue Jun 30, 2020 · 5 comments
Closed

Use Draft<S> from immer in TUpdateFunction #48

blutorange opened this issue Jun 30, 2020 · 5 comments

Comments

@blutorange
Copy link

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;
@lostpebble
Copy link
Owner

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!

@lostpebble
Copy link
Owner

I've added it and published 1.15.3 with the new type. Let me know if you run into any further issues with it.

@blutorange
Copy link
Author

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

@blutorange
Copy link
Author

Could you also update the draft argument for TReactionFunction or is that something else ? https://github.com/lostpebble/pullstate/blob/master/src/Store.ts#L28

@lostpebble
Copy link
Owner

Ah yea, that is the same. Added it and released in 1.15.4 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants