我正在构建一个 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/