unified-signatures
Disallow two overloads that could be unified into one with a union or an optional/rest parameter.
🔒
Extending "plugin:@typescript-eslint/strict"
in an ESLint configuration enables this rule.
Function overload signatures are a TypeScript way to define a function that can be called in multiple very different ways. Overload signatures add syntax and theoretical bloat, so it's generally best to avoid using them when possible. Switching to union types and/or optional or rest parameters can often avoid the need for overload signatures.
This rule reports when function overload signatures can be replaced by a single function signature.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/unified-signatures": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
function x(x: number): void;
function x(x: string): void;
function y(): void;
function y(...x: number[]): void;
function x(x: number | string): void;
function y(...x: number[]): void;
// This rule won't check overload signatures with different rest parameter types.
// See https://github.com/microsoft/TypeScript/issues/5077
function f(...a: number[]): void;
function f(...a: string[]): void;
Options
This rule accepts the following options
type Options = [
{
/** Whether two parameters with different names at the same index should be considered different even if their types are the same. */
ignoreDifferentlyNamedParameters?: boolean;
},
];
const defaultOptions: Options = [{ ignoreDifferentlyNamedParameters: false }];
ignoreDifferentlyNamedParameters
Examples of code for this rule with ignoreDifferentlyNamedParameters
:
- ❌ Incorrect
- ✅ Correct
function f(a: number): void;
function f(a: string): void;
function f(a: number): void;
function f(b: string): void;