我希望强制执行do显式声明代码中所有函数和方法的返回类型。
所以我设置了'@typescript-eslint/explicit-function-return-type': 'error'
。但是并不总是工作:
这是好的,eslint
抛出一个错误:
const obj = {
fn() { // <---- no return type, so "Missing return type on function" error
return 2;
}
};
这是不对的,eslint
在这里没有看到任何问题:
const obj: Record<string, any> = { // <---- But if I declare a type of an object...
fn() { // <---- ...this is throws no error anymore!
return 2;
}
};
如何确保在没有显式返回类型定义的情况下没有方法?
发布于 2022-07-16 15:08:32
您使用的规则有一个名为allowTypedFunctionExpressions
的选项,可以显式地将其设置为false
以获得所需的内容:
...
"rules": {
...
"@typescript-eslint/explicit-function-return-type": ["error", { "allowTypedFunctionExpressions": false }],
...
},
...
https://stackoverflow.com/questions/72981309
复制相似问题