Why do I get "Avoid referencing unbound methods which may cause unintentional scoping of `this`. eslint@typescript-eslint/unbound-method" in this case?

See original GitHub issue

I’m using Svelte 3, Typescript and this code:

  • loginStore.ts:
import { writable } from "svelte/store";

const store = () => {
  const state = {
    isLoggedIn: false,
  };

  const { subscribe, update } = writable(state); // warning: Avoid referencing unbound methods which may cause unintentional scoping of `this`. eslint@typescript-eslint/unbound-method (2 times)

  const methods = {
    OnInit() {
      this.Login(); // warning: Unsafe member access .Login on an any value. eslint@typescript-eslint/no-unsafe-member-access and another warning: Unsafe call of an any typed value. eslint@typescript-eslint/no-unsafe-call
    },

    Login() {
      if (window.localStorage.isLoggedIn) {
        update(() => {
          state.isLoggedIn = true;
          return state;
        });
      }
    },

    async Logout() {
      await doLogout();
      update(() => {
        state.isLoggedIn = false;
        return state;
      });
    },
  };

  return {
    subscribe,
    ...methods,
  };
};

export default store();

But @typescript-eslint is giving me these errors:

  1. Avoid referencing unbound methods which may cause unintentional scoping of this 2 times on line:

    const { subscribe, update } = writable(state)
    
  2. Unsafe member access .Login on an any value on line:

    this.Login()
    
  3. Unsafe call of an any typed value on line:

    this.Login()
    

How can I fix these warnings?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
dummdidummcommented, Mar 18, 2021

No, that’s up to the user to decide how strict he wants his type checking.

0reactions
frederikhorscommented, Mar 18, 2021

I can close this. You people are wonderful people!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoid referencing unbound methods which may cause ...
angular - Avoid referencing unbound methods which may cause unintentional scoping of `this` error when calling static methods - Stack Overflow. ...
Read more >
unbound-method | typescript-eslint
Enforce unbound methods are called with their expected scope. ... This rule reports when a class method is referenced in an unbound manner....
Read more >
no-unbound-method - Rule
Warns when a method is used outside of a method call. Rationale. Class functions don't preserve the class scope when passed as standalone...
Read more >
JS-0387 · Prefer that unbound methods are called with their ...
Avoid referencing unbound methods which may cause unintentional scoping of this . If your function does not access this , you can annotate...
Read more >
Avoid referencing unbound methods which may cause ...
[Solved]-Avoid referencing unbound methods which may cause unintentional scoping of `this` error when calling static methods-angular.js · turn off this lint rule ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found