Skip to main content

explicit-module-boundary-types

Require explicit return and argument types on exported functions' and classes' public class methods.

函数返回值和参数的显式类型使任何调用代码都清楚地知道模块边界的输入和输出是什么。为这些类型添加显式类型注释有助于提高代码可读性。它还可以提高较大代码库上的 TypeScript 类型检查性能。

¥Explicit types for function return values and arguments makes it clear to any calling code what is the module boundary's input and output. Adding explicit type annotations for those types can help improve code readability. It can also improve TypeScript type checking performance on larger codebases.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/explicit-module-boundary-types": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

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

选项

该规则接受以下选项:

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 return type annotations on functions with overload signatures. */
allowOverloadFunctions?: 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,
allowOverloadFunctions: 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-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

Whether to ignore arguments that are explicitly typed as any. Default: false.

带有 { allowArgumentsExplicitlyTypedAsAny: false } 的此规则的代码示例:

¥Examples of code for this rule with { allowArgumentsExplicitlyTypedAsAny: false }:

export const func = (value: any): number => value + 1;
Open in Playground

allowDirectConstAssertionInArrowFunctions

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. Default: true.

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

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

export const func = (value: number) => ({ type: 'X', value });
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
Open in Playground

allowedNames

An array of function/method names that will not have their arguments or return values checked. Default: [].

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

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

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

allowHigherOrderFunctions

Whether to ignore return type annotations on functions immediately returning another function expression. You must still type the parameters of the function. Default: true.

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

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

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

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

export function foo(outer: string) {
return function (inner: string) {};
}
Open in Playground

allowTypedFunctionExpressions

Whether to ignore type annotations on the variable of a function expression. Default: true.

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

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

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

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

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

export const foo = bar => {};
Open in Playground

allowOverloadFunctions

Whether to ignore return type annotations on functions with overload signatures. Default: false.

allowOverloadFunctions 设置为 true 时的正确代码示例:

¥Examples of correct code when allowOverloadFunctions is set to true:

export function test(a: string): string;
export function test(a: number): number;
export function test(a: unknown) {
return a;
}
Open in Playground

何时不使用它

¥When Not To Use It

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

¥If your project is not used by downstream consumers that are sensitive to API types, you can disable this rule.

进一步阅读

¥Further Reading

相关

¥Related To

'## 资源'