Skip to main content

explicit-function-return-type

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

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

¥Functions in TypeScript often don't need to be given an explicit return type annotation. Leaving off the return type is less code to read or write and allows the compiler to infer it from the contents of the function.

然而,显式返回类型确实使函数返回的类型在视觉上更加清晰。它们还可以提高具有许多大型函数的大型代码库中的 TypeScript 类型检查性能。

¥However, explicit return types do make it visually more clear what type is returned by a function. They can also speed up TypeScript type checking performance in large codebases with many large functions.

此规则强制函数确实具有显式返回类型注释。

¥This rule enforces that functions do have an explicit return type annotation.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/explicit-function-return-type": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

// 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;
}
}
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** 是否允许以 `void` 关键字开头的箭头函数。 */
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
/** 是否忽略立即返回 `as const` 值的箭头函数。 */
allowDirectConstAssertionInArrowFunctions?: boolean;
/** 是否忽略函数表达式(不属于声明的函数)。 */
allowExpressions?: boolean;
/** Whether to ignore functions that don't have generic type parameters. */
allowFunctionsWithoutTypeParameters?: boolean;
/** 是否忽略立即返回另一个函数表达式的函数。 */
allowHigherOrderFunctions?: boolean;
/** 是否忽略立即调用的函数表达式 (IIFE)。 */
allowIIFEs?: boolean;
/** 是否忽略函数表达式变量上的类型注释。 */
allowTypedFunctionExpressions?: boolean;
/** 不会检查其参数或返回值的函数/方法名称数组。 */
allowedNames?: string[];
},
];

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

¥Options

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

¥Configuring in a mixed JS/TS codebase

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

¥If you are working on a codebase within which you lint non-TypeScript code (i.e. .js/.mjs/.cjs/.jsx), you should ensure that you should use ESLint overrides to only enable the rule on .ts/.mts/.cts/.tsx files. If you don't, then you will get unfixable lint errors reported within .js/.mjs/.cjs/.jsx files.

{
"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

是否忽略函数表达式(不属于声明的函数)。 Default: false.

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

¥Examples of code for this rule with { allowExpressions: true }:

function test() {}

const fn = () => {};

export default () => {};
Open in Playground

allowTypedFunctionExpressions

是否忽略函数表达式变量上的类型注释。 Default: true.

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

¥Examples of code for this rule with { allowTypedFunctionExpressions: true }:

let arrowFn = () => 'test';

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

let objectProp = {
foo: () => 1,
};
Open in Playground

allowHigherOrderFunctions

是否忽略立即返回另一个函数表达式的函数。 Default: true.

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

¥Examples of code for this rule with { allowHigherOrderFunctions: true }:

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

function fn() {
return function () {};
}
Open in Playground

allowDirectConstAssertionInArrowFunctions

是否忽略立即返回 as const 值的箭头函数。 Default: true.

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

¥Examples of code for this rule with { allowDirectConstAssertionInArrowFunctions: true }:

const func = (value: number) => ({ type: 'X', value }) as any;
const func = (value: number) => ({ type: 'X', value }) as Action;
Open in Playground

allowConciseArrowFunctionExpressionsStartingWithVoid

是否允许以 void 关键字开头的箭头函数。 Default: false.

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

¥Examples of code for this rule with { allowConciseArrowFunctionExpressionsStartingWithVoid: true }:

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

const log = (message: string) => {
console.log(message);
};
Open in Playground

allowFunctionsWithoutTypeParameters

Whether to ignore functions that don't have generic type parameters. Default: false.

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

¥Examples of code for this rule with { allowFunctionsWithoutTypeParameters: true }:

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

const bar = <T>(t: T) => t;
Open in Playground

allowedNames

不会检查其参数或返回值的函数/方法名称数组。 Default: [].

你可以传递你希望此规则忽略的函数/方法名称,如下所示:

¥You may pass function/method names you would like this rule to ignore, like so:

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

allowIIFEs

是否忽略立即调用的函数表达式 (IIFE)。 Default: false.

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

¥Examples of code for this rule with { allowIIFEs: true }:

var func = () => 'foo';
Open in Playground

何时不使用它

¥When Not To Use It

如果你认为显式编写函数返回类型所增加的成本不值得视觉清晰度,或者你的项目不够大,不足以成为类型检查性能的一个因素,那么你将不需要此规则。

¥If you don't find the added cost of explicitly writing function return types to be worth the visual clarity, or your project is not large enough for it to be a factor in type checking performance, then you will not need this rule.

进一步阅读

¥Further Reading

资源