显式函数返回类型
要求函数和类方法显式返回类型。
TypeScript 中的函数通常不需要显式返回类型注解。省略返回类型可以减少代码阅读或编写量,并允许编译器从函数内容推断出返回类型。
但是,显式返回类型确实使函数返回的类型更加直观。它们还可以加快 TypeScript 类型检查在大型代码库中对许多大型函数的性能。
此规则强制执行函数必须具有显式返回类型注解。
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;
}
}
在游乐场中打开// No return value should be expected (void)
function test(): void {
return;
}
// A return value of type number
var fn = function (): number {
return 1;
};
// A return value of type string
var arrowFn = (): string => 'test';
class Test {
// No return value should be expected (void)
method(): void {
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 }
的代码示例
- ❌ 错误
- ✅ 正确
allowTypedFunctionExpressions
此规则使用 { allowTypedFunctionExpressions: true }
的代码示例
- ❌ 错误
- ✅ 正确
let arrowFn = () => 'test';
let funcExpr = function () {
return 'test';
};
let objectProp = {
foo: () => 1,
};
在游乐场中打开type FuncType = () => string;
let arrowFn: FuncType = () => 'test';
let funcExpr: FuncType = function () {
return 'test';
};
let asTyped = (() => '') as () => string;
let castTyped = <() => string>(() => '');
interface ObjectType {
foo(): number;
}
let objectProp: ObjectType = {
foo: () => 1,
};
let objectPropAs = {
foo: () => 1,
} as ObjectType;
let objectPropCast = <ObjectType>{
foo: () => 1,
};
declare function functionWithArg(arg: () => number);
functionWithArg(() => 1);
declare function functionWithObjectArg(arg: { method: () => number });
functionWithObjectArg({
method() {
return 1;
},
});
在游乐场中打开allowHigherOrderFunctions
此规则使用 { allowHigherOrderFunctions: true }
的代码示例
- ❌ 错误
- ✅ 正确
allowDirectConstAssertionInArrowFunctions
带有 { allowDirectConstAssertionInArrowFunctions: true }
的此规则的代码示例
- ❌ 错误
- ✅ 正确
allowConciseArrowFunctionExpressionsStartingWithVoid
带有 { allowConciseArrowFunctionExpressionsStartingWithVoid: true }
的此规则的代码示例
- ❌ 错误
- ✅ 正确
allowFunctionsWithoutTypeParameters
带有 { allowFunctionsWithoutTypeParameters: true }
的此规则的代码示例
- ❌ 错误
- ✅ 正确
allowedNames
您可以传递您希望此规则忽略的函数/方法名称,例如
{
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}
allowIIFEs
带有 { allowIIFEs: true }
的此规则的代码示例
- ❌ 错误
- ✅ 正确
何时不使用它
如果您认为显式编写函数返回类型带来的额外成本不值得视觉清晰度,或者您的项目规模不足以影响类型检查性能,那么您将不需要此规则。
进一步阅读
- TypeScript 函数