Dadinho
Dadinho

Reputation: 71

Why do I need to declare the return type twice?

I am building an api just for practice. In my solution I would like to provide some kind of cache functionality. Its type can be set in configuration file like writing into file or into database.

My question is rather related to the 'eslint', because during the development I am using typescript and I want to linting my source code before the commit.

I cannot understand the point of this warning message during the linting:

9:14 warning Missing return type on function @typescript-eslint/explicit-function-return-type

I would like to avoid the duplication and use everything in the right place.

If I set again the return type like as at the check method then the message is gone. But in that case I cannot see the point to set it in the interface.

interface CacheSolution {
  get(key: string): string;
  write(key: string, value: string): void;
  check(key: string): boolean;
}

class CacheIntoDatabase implements CacheSolution {

  public get (key) {
    return 'something';
  }

  public write (key: string, value: string) {

  }

  public check (key: string): boolean {
    return true;
  }

}

ESLINT configuration

module.exports = {
  'env': {
    'es6': true,
    'node': true
  },
  'extends': [
      'plugin:@typescript-eslint/recommended'
  ],
  'globals': {
    'Atomics': 'readonly',
    'SharedArrayBuffer': 'readonly'
  },
  'parser':  '@typescript-eslint/parser',
  'parserOptions': {
    'ecmaVersion': 2018,
    'sourceType': 'module'
  },
  'rules': {
    'indent': 'off',
    '@typescript-eslint/indent': ['error', 2]
  }
};

Can I solve the warning somehow or I need to try live together with the duplication? :)

Upvotes: 0

Views: 287

Answers (1)

Alekhine
Alekhine

Reputation: 26

Change this

public write (key: string, value: string) {

to this

public write (key: string, value: string):void {

The reason you are getting this error probably has to do with you lint rules having a rule requiring method return types, see this answer for more details on that.

Upvotes: 1

Related Questions