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

Improving recursive typings when TypeScript 3.7 is out #448

Closed
mweststrate opened this issue Oct 30, 2019 · 1 comment
Closed

Improving recursive typings when TypeScript 3.7 is out #448

mweststrate opened this issue Oct 30, 2019 · 1 comment

Comments

@mweststrate
Copy link
Collaborator

Waiting for TS 3.7 to be released

The types for Immutable<T> and Draft<T> are not as accurate as we would love them to be. This is largely caused by TypeScript not yet supporting recursive types. 3.7 should change that, so it would be a good time to revisit these types.

[ ] Improve types as suggested in f8c65f0
[ ] Re-evaluate #385
[ ] Fall back to the classic TS recursivity trick?

mweststrate added a commit that referenced this issue Oct 30, 2019
BREAKING CHANGE: Maps and Sets are treated differently now; they will no longer directly mutated when updated inside a draft

This release introduces first class support for Maps and Sets! 
Some things to keep in mind:

* Inside recipes, you can directly modify Maps and Sets with methods like `add`, `set`, `delete` and `clear`
* Those methods do mutate draft Maps and Sets, but won't actually change their originals!
* Immer does not polyfill Map and Set automatically in environments where those aren't available out of the box
* Maps and Sets are supported both in ES5 and Proxy mode
* If `autoFreeze` is enabled, the maps and sets returned from a producer will be artificially frozen by making their mutative APIs unusable
* Non primitive keys for Maps, and non primitive values for Sets are supported. However, we strongly recommend to not combine non-primitive keys to Maps with patches, for reasons expressed below. 

Open questions
* TypeScript support for storing immutable types inside Maps and Sets, and converting them to `Draft`'s, is limited, see #448 for details
* Since JSON-patch standard doesn't offer support for Sets or Maps, it is not entirely clear how mutations to those are best described by patches, so this might be refined in the future. See also #450  

Credits to @runnez, @aigoncharov  and @aleclarson for making this happen!
@antonyoneill
Copy link

Hey, just a heads up that typescript 3.7 is out 🎉

https://github.com/microsoft/TypeScript/releases/tag/v3.7.2

Mossop added a commit to Mossop/immer that referenced this issue Jan 3, 2020
…ixes immerjs#494.

TypeScript provides the ReadonlySet and ReadonlyMap types which match
their normal counterparts but without any methods that mutate their data.
This change makes Immutable<Map> and Immutable<Set> map to those types
directly.

Worth noting that Map extends ReadonlyMap so we only need to check for
one to know what the resulting type is, the same goes for Set and
ReadonlySet.

This also removes the intermediate Set/Map types since TypeScript no
longer seems to need those (likely since TypeScript 3.7, see immerjs#448).
Mossop added a commit to Mossop/immer that referenced this issue Jan 3, 2020
…ixes immerjs#494.

TypeScript provides the ReadonlySet and ReadonlyMap types which match
their normal counterparts but without any methods that mutate their data.
This change makes Immutable<Map> and Immutable<Set> map to those types
directly.

Worth noting that Map extends ReadonlyMap so we only need to check for
one to know what the resulting type is, the same goes for Set and
ReadonlySet.

This also removes the intermediate Set/Map types since TypeScript no
longer seems to need those (likely since TypeScript 3.7, see immerjs#448).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants