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;
}
}
在游乐场中打开// A function with no return value (void)
export function test(): void {
return;
}
// A return value of type string
export var arrowFn = (): string => 'test';
// All arguments should be typed
export var arrowFn = (arg: string): string => `test ${arg}`;
export var arrowFn = (arg: unknown): string => `test ${arg}`;
export class Test {
// A class method with no return value (void)
method(): void {
return;
}
}
// The function does not apply because it is not an exported function.
function test() {
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 的参数。
- ❌ 对于
allowArgumentsExplicitlyTypedAsAny: false
不正确 - ✅ 对于
allowArgumentsExplicitlyTypedAsAny: true
正确
allowDirectConstAssertionInArrowFunctions
当此选项为 true
时,规则将忽略返回 as const
类型断言的无主体箭头函数的返回类型注解。
- ❌ `allowDirectConstAssertionInArrowFunctions: false` 时不正确
- ✅ `allowDirectConstAssertionInArrowFunctions: true` 时正确
export const func = (value: number) => ({ type: 'X', value });
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
在游乐场中打开export const func = (value: number) => ({ type: 'X', value }) as const;
export const foo = () =>
({
bar: true,
}) as const;
export const bar = () => 1 as const;
在游乐场中打开allowedNames
您可以传递您希望此规则忽略的函数/方法名称,例如
{
"@typescript-eslint/explicit-module-boundary-types": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}
allowHigherOrderFunctions
当此选项为 true
时,该规则会忽略立即返回另一个函数表达式的函数的返回类型注释。
- ❌ `allowHigherOrderFunctions: false` 时不正确
- ✅ `allowHigherOrderFunctions: true` 时正确
export const arrowFn = () => () => {};
export function fn() {
return function () {};
}
export function foo(outer: string) {
return function (inner: string) {};
}
在游乐场中打开export const arrowFn = () => (): void => {};
export function fn() {
return function (): void {};
}
export function foo(outer: string) {
return function (inner: string): void {};
}
在游乐场中打开allowTypedFunctionExpressions
当此选项为 true
时,该规则会忽略函数表达式变量的类型注释。
- ❌ `allowTypedFunctionExpressions: false` 时不正确
- ✅ `allowTypedFunctionExpressions: true` 时正确
export let arrowFn = () => 'test';
export let funcExpr = function () {
return 'test';
};
export let objectProp = {
foo: () => 1,
};
export const foo = bar => {};
在游乐场中打开type FuncType = () => string;
export let arrowFn: FuncType = () => 'test';
export let funcExpr: FuncType = function () {
return 'test';
};
export let asTyped = (() => '') as () => string;
export let castTyped = <() => string>(() => '');
interface ObjectType {
foo(): number;
}
export let objectProp: ObjectType = {
foo: () => 1,
};
export let objectPropAs = {
foo: () => 1,
} as ObjectType;
export let objectPropCast = <ObjectType>{
foo: () => 1,
};
type FooType = (bar: string) => void;
export const foo: FooType = bar => {};
在游乐场中打开何时不使用它
如果您的项目没有被对 API 类型敏感的下游消费者使用,您可以禁用此规则。
进一步阅读
- TypeScript 函数