javascript - 为什么我需要声明两次返回类型?

标签 javascript typescript eslint

我正在构建一个 api 只是为了练习。 在我的解决方案中,我想提供某种缓存功能。其类型可以在配置文件中设置,如写入文件或数据库。

我的问题与“eslint”相当相关,因为在开发过程中我使用 typescript ,并且我想在提交之前检查我的源代码。

我无法理解此警告消息在 linting 过程中的意义:

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

我想避免重复并在正确的位置使用所有内容。

如果我像检查方法一样再次设置返回类型,那么消息就会消失。但在这种情况下,我看不到在界面中设置它的意义。

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 配置

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]
  }
};

我可以以某种方式解决这个警告吗?或者我需要尝试与重复一起生活? :)

最佳答案

改变这个

公共(public)写入(键:字符串,值:字符串){

到此

public write(键:字符串,值:字符串):void {

您收到此错误的原因可能与您的 lint 规则有关,该规则具有需要方法返回类型的规则,请参阅 this answer了解更多详细信息。

关于javascript - 为什么我需要声明两次返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56973045/

相关文章:

angular - 为什么 typescript-eslint 强制 enumMember 使用驼峰命名法?

javascript - 强制执行延迟 Controller 逻辑

javascript - 将回调添加到动画队列 - jQuery

javascript - 在 Subscribe 函数中从 observableArray 中删除元素,而无需再次发出信号

javascript - 如何阻止在 contenteditable 元素中输入数字

typescript - 阿多尼斯 5 "make:migration command not found"有什么想法吗?

javascript - React 组件的 ESLint 规则 : Method Render expected no return value

javascript - 将 div 滚动到 div 的中心

javascript - 路由器在注入(inject) ngrx 存储定义解析器后停止工作

typescript - 类型注释和类型推断有什么区别?