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

[eslint plugin] Conflict between TypeScript and ESLint when private property #3992

Open
baruchiro opened this issue Jan 5, 2025 · 1 comment
Labels

Comments

@baruchiro
Copy link

Intended outcome:

I'm trying to exclude a field from being made observable using makeObservable with false annotation, as documented in the MobX documentation. The following code should work:

import { makeObservable } from "mobx";

export class MyStore {
  private count = 0;

  constructor() {
    makeObservable(this, {
      count: false,
    });
  }
}

Actual outcome:

I'm getting contradictory TypeScript/ESLint errors:

  1. When the count property is not specified in makeObservable:
Missing annotation for `count`. To exclude a field, use `false` as annotation. (ESLint)
  1. When adding count: false as suggested by the first error:
Object literal may only specify known properties, and 'count' does not exist in type 'AnnotationsMap<this, never>'. (ts)

This creates a catch-22 situation where ESLint requires the annotation but then TypeScript doesn't accept it.

How to reproduce the issue:

I've created a minimal reproduction repository that demonstrates this issue:
example-mobx-eslint-bug

Steps to reproduce:

  1. Clone the repository
  2. Install dependencies
  3. Observe the TypeScript errors in the code

Versions

"mobx": "^6.13.5",
"mobx-react-lite": "^4.1.0",
"eslint-plugin-mobx": "^0.0.13"
@NckHmml
Copy link

NckHmml commented Feb 19, 2025

Hello @baruchiro, not part of the dev team, but I can chip in on this.

First of all, the default behaviour of makeObservable is to explicitly set what you want to observe, so in your example count is never observed anyway.

However, if you meant makeAutoObservable instead, which would make count observable, you could exclude it wit the following code

makeAutoObservable<this, "count">(this, {
  count: false
});

Or this also seems to work (albeit less type-safe)

const map = {
  count: false
};
makeAutoObservable(this, map);

This is because of the behaviour of Typescript to not have private fields in keyof this, which is intended. The current solution although a bit sub-optimal, is probably the best (if not only). Changing that behaviour would probably be a feature request and not a bug.

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

2 participants