跳至主要内容

显式函数返回类型

要求函数和类方法显式返回类型。

TypeScript 中的函数通常不需要显式返回类型注解。省略返回类型可以减少代码阅读或编写量,并允许编译器从函数内容推断出返回类型。

但是,显式返回类型确实使函数返回的类型更加直观。它们还可以加快 TypeScript 类型检查在大型代码库中对许多大型函数的性能。

此规则强制执行函数必须具有显式返回类型注解。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/explicit-function-return-type": "error"
}
};

在游乐场中尝试此规则 ↗

示例

// Should indicate that no value is returned (void)
function test() {
return;
}

// Should indicate that a number is returned
var fn = function () {
return 1;
};

// Should indicate that a string is returned
var arrowFn = () => 'test';

class Test {
// Should indicate that no value is returned (void)
method() {
return;
}
}
在游乐场中打开

选项

此规则接受以下选项

type Options = [
{
/** Whether to allow arrow functions that start with the `void` keyword. */
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
/** Whether to ignore arrow functions immediately returning a `as const` value. */
allowDirectConstAssertionInArrowFunctions?: boolean;
/** Whether to ignore function expressions (functions which are not part of a declaration). */
allowExpressions?: boolean;
/** Whether to ignore functions that don't have generic type parameters. */
allowFunctionsWithoutTypeParameters?: boolean;
/** Whether to ignore functions immediately returning another function expression. */
allowHigherOrderFunctions?: boolean;
/** Whether to ignore immediately invoked function expressions (IIFEs). */
allowIIFEs?: boolean;
/** Whether to ignore type annotations on the variable of function expressions. */
allowTypedFunctionExpressions?: boolean;
/** An array of function/method names that will not have their arguments or return values checked. */
allowedNames?: string[];
},
];

const defaultOptions: Options = [
{
allowExpressions: false,
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowFunctionsWithoutTypeParameters: false,
allowedNames: [],
allowIIFEs: false,
},
];

在混合 JS/TS 代码库中配置

如果您正在处理一个代码库,在其中您对非 TypeScript 代码(即 .js/.mjs/.cjs/.jsx)进行 lint,则应确保使用 ESLint overrides 仅在 .ts/.mts/.cts/.tsx 文件上启用此规则。如果您不这样做,那么您将在 .js/.mjs/.cjs/.jsx 文件中收到无法修复的 lint 错误报告。

{
"rules": {
// disable the rule for all files
"@typescript-eslint/explicit-function-return-type": "off",
},
"overrides": [
{
// enable the rule specifically for TypeScript files
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "error",
},
},
],
}

allowExpressions

此规则使用 { allowExpressions: true } 的代码示例

function test() {}

const fn = () => {};

export default () => {};
在游乐场中打开

allowTypedFunctionExpressions

此规则使用 { allowTypedFunctionExpressions: true } 的代码示例

let arrowFn = () => 'test';

let funcExpr = function () {
return 'test';
};

let objectProp = {
foo: () => 1,
};
在游乐场中打开

allowHigherOrderFunctions

此规则使用 { allowHigherOrderFunctions: true } 的代码示例

var arrowFn = () => () => {};

function fn() {
return function () {};
}
在游乐场中打开

allowDirectConstAssertionInArrowFunctions

带有 { allowDirectConstAssertionInArrowFunctions: true } 的此规则的代码示例

const func = (value: number) => ({ type: 'X', value }) as any;
const func = (value: number) => ({ type: 'X', value }) as Action;
在游乐场中打开

allowConciseArrowFunctionExpressionsStartingWithVoid

带有 { allowConciseArrowFunctionExpressionsStartingWithVoid: true } 的此规则的代码示例

var join = (a: string, b: string) => `${a}${b}`;

const log = (message: string) => {
console.log(message);
};
在游乐场中打开

allowFunctionsWithoutTypeParameters

带有 { allowFunctionsWithoutTypeParameters: true } 的此规则的代码示例

function foo<T>(t: T) {
return t;
}

const bar = <T>(t: T) => t;
在游乐场中打开

allowedNames

您可以传递您希望此规则忽略的函数/方法名称,例如

{
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}

allowIIFEs

带有 { allowIIFEs: true } 的此规则的代码示例

var func = () => 'foo';
在游乐场中打开

何时不使用它

如果您认为显式编写函数返回类型带来的额外成本不值得视觉清晰度,或者您的项目规模不足以影响类型检查性能,那么您将不需要此规则。

进一步阅读

资源