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

Property ... is missing in type WritableDraft<...> but required in type ... #1097

Closed
2 tasks
Rennix09 opened this issue Mar 6, 2024 · 1 comment
Closed
2 tasks
Labels

Comments

@Rennix09
Copy link

Rennix09 commented Mar 6, 2024

🙋‍♂ Question

I got a type error when I tried to wrap a class instance that owns private fields. WhatI should I do?

Code Snippet

import { WritableDraft } from "immer/dist/internal";

class Frontend {
    private frameworks: string[] = ['React', 'Vue', 'Angular'];
}

const frontend: Frontend = {} as WritableDraft<Frontend>; 

Expect:
The wrapped type is compatible to the original type

Actual:
Property 'frameworks' is missing in type 'WritableDraft' but required in type 'Frontend'.ts(2741)

  • Immer version:
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@Rennix09
Copy link
Author

Rennix09 commented Mar 8, 2024

Resolved this by defining an interface.

import { WritableDraft } from "immer/dist/internal";
interface IFrontend {
  ... // all the public members here
}

class Frontend implements IFrontend {
    private frameworks: string[] = ['React', 'Vue', 'Angular'];
}

const frontend: IFrontend = {} as WritableDraft<IFrontend>; 

@Rennix09 Rennix09 closed this as completed Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant