immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to type immerable class

st-clair-clarke opened this issue Β· comments

πŸ™‹β€β™‚ Question

I have the following that is using Typescript strict environment.

export interface IUiState {
   id: string
}

export class UiState {
   id: string = nanoid()
}

UiState[immerable] = true

My IDE (Webstorm) generates a linting error from the statement RegistrationUiState[immerable] = true as shown below:

TS7053: Element implicitly has an 'any' type because expression of type 'unique symbol' can't be used to index type 'typeof 
RegistrationState'.   Property '["immer/dist/utils/env".DRAFTABLE]' does not exist on type 'typeof RegistrationState'.

How can I explicitly type the above to correct the issue?

Environment

We only accept questions against the latest Immer version.

  • Immer version:
    "immer": "^9.0.19",
    "typescript": "^4.9.5",
    "@angular/cli": "^15.2.2",

You can use

export class UiState {
   id: string = nanoid()
   [immerable] = true
}

Closing as answered