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

Support Typescript's readonly array, map and set types #494

Closed
Mossop opened this issue Jan 2, 2020 · 2 comments
Closed

Support Typescript's readonly array, map and set types #494

Mossop opened this issue Jan 2, 2020 · 2 comments

Comments

@Mossop
Copy link
Contributor

Mossop commented Jan 2, 2020

🚀 Feature Proposal

Typescript has some utility types, ReadonlyArray, ReadonlySet and ReadonlyMap<K, V> but immer doesn't seem to correctly handle these in the Immutable and Draft types.

Motivation

More consistent type checking is in everyone's best interests!

Can this be solved in user-land code?

I could probably write my own typings to override immer's but I would expect this to be something included in immer itself.

Example

import { Draft, produce } from "immer";

let foo: ReadonlySet<string> = new Set();

produce(foo, (draft: Draft<ReadonlySet<string>>) => {
  draft.set("bar");
});

Result:

`Property 'set' does not exist on type '{ forEach: (callbackfn: (value: string, value2: string, set: ReadonlySet) => void, thisArg?: any) => void; has: (value: string) => boolean; size: number; entries: { ...; }; keys: { ...; }; values: { ...; }; }'

@Mossop Mossop added the proposal label Jan 2, 2020
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).
@mweststrate
Copy link
Collaborator

Implemented through #495 -> #506

@aleclarson
Copy link
Member

🎉 This issue has been resolved in version 5.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

3 participants