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

Typescript error when using classes with private properties #137

Open
dmm9 opened this issue Aug 30, 2024 · 0 comments
Open

Typescript error when using classes with private properties #137

dmm9 opened this issue Aug 30, 2024 · 0 comments

Comments

@dmm9
Copy link

dmm9 commented Aug 30, 2024

useImmerReducer() shows a type error when using classes with private properties:

class ClassWithPrivates {
  private readonly pa: string;
  private pb: boolean;
  private pmethod() {...}
  ...
}

interface myState {
  items: ClassWithPrivates[]
}
interface myAction {
  type: string;
  ...
}

function reducer(draft: myState, action: myAction) { // <-- Also 'draft: Draft<myState>' won't work
  switch (action.type) {
  ...
  }
}

export function myComponent() {
  const [state, dispatch] = useImmerReducer(reducer, initialValues);  // <-- Error in 'reducer'
  return (...)
}

The error:

Argument of type '(draft: myState, action: myAction) => myState' is not assignable to parameter of type 'ImmerReducer<myState, myAction>'.
  Types of parameters 'draft' and 'draftState' are incompatible.
    Type 'WritableDraft<myState>' is not assignable to type 'myState'.
      Types of property 'items' are incompatible.
        Type 'WritableDraft<ClassWithPrivates>' is missing the following properties from type 'ClassWithPrivates': pa, pb, pmethod ts(2345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant