How can I fix this @typescript-eslint warning using an arrow function with "this" inside?

Viewed 250

I'm using this typescript code:

const tryThis = () => {
  const methods = {
    Login() {
      console.log("I'm Login()")
    },

    OnInit() {
      this.Login()
    }
  }

  return methods
}

but @typescript-eslint warns me with:

  1. Unsafe member access .Login on an any value. eslint@typescript-eslint/no-unsafe-member-access

  2. Unsafe call of an any typed value. eslint@typescript-eslint/no-unsafe-call

Why?

How can I fix this?

1 Answers

Depending on your eslint config, the linter might not allow access to members with any type. To fix that you can fix that in your .eslintrc.js file with "no-unsafe-any": false check here, or type your function.

type ThisType = () => {
    Login(): void;
    OnInit(): void;
}

const tryThis:ThisType = () => {
  const methods = {
    Login() {
      console.log("I'm Login()")
    },

    OnInit() {
      this.Login()
    }
  }

  return methods
}
Related