跳至主要内容

explicit-module-boundary-types

要求在导出函数和类的公共类方法上显式返回和参数类型。

为函数返回值和参数显式指定类型,可以让任何调用代码清楚地了解模块边界的输入和输出。为这些类型添加显式类型注解可以帮助提高代码可读性。它还可以提高 TypeScript 类型检查在大型代码库上的性能。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "error"
}
};

在游乐场中尝试此规则 ↗

示例

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

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

// All arguments should be typed
export var arrowFn = (arg): string => `test ${arg}`;
export var arrowFn = (arg: any): string => `test ${arg}`;

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

选项

此规则接受以下选项

type Options = [
{
/** Whether to ignore arguments that are explicitly typed as `any`. */
allowArgumentsExplicitlyTypedAsAny?: boolean;
/**
* Whether to ignore return type annotations on body-less arrow functions that return an `as const` type assertion.
* You must still type the parameters of the function.
*/
allowDirectConstAssertionInArrowFunctions?: boolean;
/**
* Whether to ignore return type annotations on functions immediately returning another function expression.
* You must still type the parameters of the function.
*/
allowHigherOrderFunctions?: boolean;
/** Whether to ignore type annotations on the variable of a function expression. */
allowTypedFunctionExpressions?: boolean;
/** An array of function/method names that will not have their arguments or return values checked. */
allowedNames?: string[];
},
];

const defaultOptions: Options = [
{
allowArgumentsExplicitlyTypedAsAny: false,
allowDirectConstAssertionInArrowFunctions: true,
allowedNames: [],
allowHigherOrderFunctions: true,
allowTypedFunctionExpressions: true,
},
];

在混合 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-module-boundary-types": "off",
},
"overrides": [
{
// enable the rule specifically for TypeScript files
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "error",
},
},
],
}

allowArgumentsExplicitlyTypedAsAny

当此选项为 true 时,规则将忽略显式类型为 any 的参数。

export const func = (value: any): number => value + 1;
在游乐场中打开

allowDirectConstAssertionInArrowFunctions

当此选项为 true 时,规则将忽略返回 as const 类型断言的无主体箭头函数的返回类型注解。

export const func = (value: number) => ({ type: 'X', value });
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
在游乐场中打开

allowedNames

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

{
"@typescript-eslint/explicit-module-boundary-types": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}

allowHigherOrderFunctions

当此选项为 true 时,该规则会忽略立即返回另一个函数表达式的函数的返回类型注释。

export const arrowFn = () => () => {};

export function fn() {
return function () {};
}

export function foo(outer: string) {
return function (inner: string) {};
}
在游乐场中打开

allowTypedFunctionExpressions

当此选项为 true 时,该规则会忽略函数表达式变量的类型注释。

export let arrowFn = () => 'test';

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

export let objectProp = {
foo: () => 1,
};

export const foo = bar => {};
在游乐场中打开

何时不使用它

如果您的项目没有被对 API 类型敏感的下游消费者使用,您可以禁用此规则。

进一步阅读

资源