Typescript 有没有类似“的规则?”;显式函数返回类型";不需要空白类型?
是否有任何ESLint规则与 [显式函数返回类型]但不需要函数上的void类型Typescript 有没有类似“的规则?”;显式函数返回类型";不需要空白类型?,typescript,eslint,Typescript,Eslint,是否有任何ESLint规则与 [显式函数返回类型]但不需要函数上的void类型 如果没有,我应该如何执行此自定义规则?对于那些需要此规则的人,我已设法执行此自定义规则,但它目前仅适用于类内的方法。 我使用的角度应用,所以它的工作刚刚好 const expUtil = require("@typescript-eslint/experimental-utils"); module.exports = (context) => { /** * Checks if a node is
如果没有,我应该如何执行此自定义规则?对于那些需要此规则的人,我已设法执行此自定义规则,但它目前仅适用于类内的方法。 我使用的角度应用,所以它的工作刚刚好
const expUtil = require("@typescript-eslint/experimental-utils");
module.exports = (context) => {
/**
* Checks if a node is a constructor.
* @param node The node to check
*/
function isConstructor(node) {
return (!!node &&
node.type === expUtil.AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor');
}
/**
* Checks if a node is a setter.
*/
function isSetter(node) {
return (!!node &&
(node.type === expUtil.AST_NODE_TYPES.MethodDefinition ||
node.type === expUtil.AST_NODE_TYPES.Property) &&
node.kind === 'set');
}
/**
* Checks if a node is returning some value
*/
function isReturningValue(node) {
const content = node.value.body;
const returnVal = content.body.find(n =>
n.type === expUtil.AST_NODE_TYPES.ReturnStatement
);
return (returnVal && !!returnVal.argument);
}
/**
* Checks if a function declaration/expression has a return type.
*/
function checkFunctionReturnType(node) {
if (node.returnType ||
node.value.returnType ||
isConstructor(node.parent) ||
isSetter(node.parent)) {
return;
}
if (!isReturningValue(node)) {
return;
}
context.report(
node, 'Functions of type void should not return any value'
);
}
return {
MethodDefinition: checkFunctionReturnType
};
};