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

Add new type: Mutable #27

Closed
emilgpa opened this issue Jul 31, 2018 · 10 comments · Fixed by #111
Closed

Add new type: Mutable #27

emilgpa opened this issue Jul 31, 2018 · 10 comments · Fixed by #111

Comments

@emilgpa
Copy link

emilgpa commented Jul 31, 2018

Issuehunt badges

I think that there are not a type for mutable object

export type Mutable<T> = {
    -readonly [P in keyof T]: T[P];
};

IssueHunt Summary

dosentmatter dosentmatter has been rewarded.

Backers (Total: $20.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

@piotrwitek
Copy link
Owner

piotrwitek commented Aug 2, 2018

Interesting, will figure out a bit later, have a lot on my plate right now.

@piotrwitek
Copy link
Owner

piotrwitek commented Sep 13, 2018

I'm not sure about this one, like is it something really useful?
I would like to know who else gonna need it. I'm mainly using react with functional programming paradigms (I'm not using mutation at all) so it's hard to tell if that would be something useful. At least I never had a single use case to use something like that.

@toverux
Copy link

toverux commented Jan 25, 2019

I once used it in an Angular app.
I use the container/component pattern and mostly work with immutable data. Notably, data coming from the API is marked readonly (and objects coming from Apollo should not be modified since they come from its internal Redux cache).

But in some presentational document I had to mutate an object for two-way binding purposes. So I derived the original type with -readonly, and the presentational component was copying the received readonly-marked object with { ...obj } to create a temporary, safely mutable copy.

Since then I had refactored that part and it no longer exists. That was the only occurrence. So yes it's pretty rare.

@emilgpa
Copy link
Author

emilgpa commented Jan 30, 2019

const copy = Object.assign({}, this.state); // copy the state of react
copy.options = []; // raise error "TS2540: Cannot assign to 'options' because it is a read-only property"
this.setState({...copy})

It is fixed as follows:

const copy: Mutable<IState> = Object.assign({}, this.state); // copy the state of react
copy.options = []; // not raise error here now
this.setState({...copy})

or

const copy = Object.assign<{}, IState>({}, this.state);

There may be other scenarios but yes, it are not usual cases.

@levenleven
Copy link
Contributor

@emilgpa this can be fixed by just const copy = { ...this.state };

@emilgpa
Copy link
Author

emilgpa commented Jan 30, 2019

@levenleven This is not about whether there is another way to clone an object but that there may be a scenario where you need to be able to mutate an object.
It may be that the person wants to use Object.assign for compatibility reasons (without wanting to use babel or any other tool).

@piotrwitek
Copy link
Owner

Ok let's include this type, I agree with the @emilgpa argument that there might be some cases where you would want to reverse operation of DeepReadonly helper or clone immutable object to mutate it.

Accepting PRs.

@IssueHuntBot
Copy link

@IssueHunt has funded $20.00 to this issue.


@piotrwitek piotrwitek changed the title Include Mutable type Add new type: Mutable Apr 28, 2019
dosentmatter added a commit to dosentmatter/utility-types that referenced this issue Oct 21, 2019
dosentmatter added a commit to dosentmatter/utility-types that referenced this issue Oct 21, 2019
@dosentmatter dosentmatter mentioned this issue Oct 21, 2019
6 tasks
dosentmatter added a commit to dosentmatter/utility-types that referenced this issue Oct 21, 2019
dosentmatter added a commit to dosentmatter/utility-types that referenced this issue Oct 22, 2019
piotrwitek pushed a commit that referenced this issue Oct 22, 2019
* Add `Mutable` type

Resolves: #27

* Rename `WritableKeys<T>` to `MutableKeys<T>`

Keep `WritableKeys<T>` as an alias to `MutableKeys<T>`.

Issue: #111
@issuehunt-oss
Copy link

issuehunt-oss bot commented Oct 22, 2019

@piotrwitek has rewarded $14.00 to @dosentmatter. See it on IssueHunt

  • 💰 Total deposit: $20.00
  • 🎉 Repository reward(20%): $4.00
  • 🔧 Service fee(10%): $2.00

@issuehunt-oss issuehunt-oss bot added the 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt label Oct 22, 2019
@SimenB
Copy link

SimenB commented Oct 29, 2019

Would be lovely to have DeepMutable as well 🙂 To mirror DeepReadonly

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

Successfully merging a pull request may close this issue.

6 participants