adjacent-overload-signatures
Require that function overload signatures be consecutive.
Extending "plugin:@typescript-eslint/stylistic" in an ESLint configuration enables this rule.
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.
- Flat Config
- Legacy Config
export default defineConfig({
rules: {
"@typescript-eslint/adjacent-overload-signatures": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
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 Playgrounddeclare namespace Foo {
export function foo(s: string): void;
export function foo(n: number): void;
export function foo(sn: string | number): void;
export function bar(): void;
}
type Foo = {
foo(s: string): void;
foo(n: number): void;
foo(sn: string | number): void;
bar(): void;
};
interface Foo {
foo(s: string): void;
foo(n: number): void;
foo(sn: string | number): void;
bar(): void;
}
class Foo {
foo(s: string): void;
foo(n: number): void;
foo(sn: string | number): void {}
bar(): void {}
}
export function bar(): void;
export function foo(s: string): void;
export function foo(n: number): void;
export function foo(sn: string | number): void;
Open in PlaygroundOptions
This rule accepts the following options:
type Options = [
{
/** Whether to ignore getters and setters, which cannot be overloaded. */
ignoreAccessors?: boolean;
},
];
const defaultOptions: Options = [{ ignoreAccessors: false }];
ignoreAccessors
Whether to ignore getters and setters, which cannot be overloaded. Default: false.
Getters and setters can't be overloaded, but by default this rule treats them as signatures of the same member, so a getter and setter with the same name must be adjacent. Enabling this option ignores accessors entirely.
Examples of correct code with { "ignoreAccessors": true }:
- ✅ Correct
class Foo {
get a(): number {
return 1;
}
get b(): number {
return 2;
}
set a(value: number) {}
set b(value: number) {}
}
Open in PlaygroundWhen Not To Use It
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.
Related To
unified-signatures- ESLint:
grouped-accessor-pairs- enforces getter and setter pairs being adjacent.