Skip to main content

adjacent-overload-signatures

Require that function overload signatures be consecutive.

🎨

ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic" 可启用此规则。


函数重载签名表示调用函数的多种方式,可能具有不同的返回类型。 描述函数的接口或类型别名通常会将所有重载签名彼此相邻放置。 如果签名放置在类型中的其他位置,则将来的开发者阅读代码时更容易遗漏。

英:Function overload signatures represent multiple ways a function can be called, potentially with different return types. It's typical for an interface or type alias describing a function to place all overload signatures next to each other. If Signatures placed elsewhere in the type are easier to be missed by future developers reading the code.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error"
}
};
在线运行试试这个规则 ↗

示例

declare namespace Foo {
export function foo(s: string): void;
export function foo(n: number): void;
export function bar(): void;
export function foo(sn: string | number): void;
}

type Foo = {
foo(s: string): void;
foo(n: number): void;
bar(): void;
foo(sn: string | number): void;
};

interface Foo {
foo(s: string): void;
foo(n: number): void;
bar(): void;
foo(sn: string | number): void;
}

class Foo {
foo(s: string): void;
foo(n: number): void;
bar(): void {}
foo(sn: string | number): void {}
}

export function foo(s: string): void;
export function foo(n: number): void;
export function bar(): void;
export function foo(sn: string | number): void;
Open in Playground

何时不使用它

有时将重载签名与类型的其他有意义的部分放在一起可能很有用。 例如,如果函数的每个重载对应于不同的属性,你可能希望将每个重载放在其对应的属性旁边。 你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

英:It can sometimes be useful to place overload signatures alongside other meaningful parts of a type. For example, if each of a function's overloads corresponds to a different property, you might wish to put each overloads next to its corresponding property. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

选项

该规则不可配置。

资源