typescriptoopoverloadingeslinttypescript-eslint

Typescript class doesn't allow static async method overloading


I'm trying to overload the login function so that when i pass in an argument to it the return type gets inferred as LoginResponse and when i don't pass any arguments it gets inferred as void | Error. I am using the singleton pattern so that's why i have the fields at the top defined like that Here is the relevant part of my class

export default class AuthAPI {
  private static instance: AuthAPI;
  private serverToken: string;
  private constructor(serverToken: string) {
    this.serverToken = serverToken;
  }
  static async login(loginData: LoginData): Promise<LoginResponse>;
  static async login(): Promise<Error | void>;
  // login method if there is loginData given
  static async login(
    loginData?: LoginData,
  ): Promise<LoginResponse | Error | void> {
    /** try sending a request with localStorage token
     * and if there's a 401 error the token isn't valid anymore.
     * then try logging in with loginData */

    // try signing in with local token in storage
    if (loginData === undefined) {
      // try sending a request and if there's a 401 error the token isn't valid anymore

      if (Math.random() * 1000 === 401) {
        return new Error("Token expired; Couldn't log in locally");
      }
      if (Math.random() * 1000 === 200) {
        return;
      }
    }
    if (loginData) {
      // if not log in with a request to the server
      return {token: 'a string', user: 'user'};
    }
  }
}

the relevant types are:

export interface LoginData {
  loginEmail?: string;
  username?: string;
  password: string;
}
export type LoginResponse = {
  token: string;
  user: string;
};

This is my eslint config

module.exports = {
  root: true,
  extends: '@react-native-community',
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  overrides: [
    {
      files: ['*.ts', '*.tsx'],
      rules: {
        '@typescript-eslint/no-shadow': ['error'],
        'no-shadow': 'off',
        'no-undef': 'off',
        'react-native/no-inline-styles': 0,
        'prettier/prettier': [
          'error',
          {
            'no-inline-styles': false,
          },
        ],
        'react/self-closing-comp': 'off',
        'no-unused-vars': 'off',
        '@typescript-eslint/no-unused-vars': 'off',
        'react-hooks/rules-of-hooks': 'off',
        'react-hooks/exhaustive-deps': 'off',
      },
    },
  ],
};

I get a Duplicate name 'login' eslint no-dupe-class-members error in vscode. How do i fix it? enter image description here

I Checked if there were multiple implementations and there aren't. I tried reproducing the problem on replit playground and there was no error. i think the problem is eslint.


Solution

  • I solved it!

    The problem was that my default eslint configuration had "no-duplicate-class-members" on. I turned it off because it isn't needed, as @jcalz said, and now it's fixed.