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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/unified-signatures": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/unified-signatures": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
function x(x: number): void;
function x(x: string): void;
Open in Playgroundfunction y(): void;
function y(...x: number[]): void;
Open in Playgroundfunction x(x: number | string): void;
Open in Playgroundfunction y(...x: number[]): void;
Open in Playground// 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;
Open in PlaygroundOptions
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
Whether two parameters with different names at the same index should be considered different even if their types are the same. Default: false
.
Examples of code for this rule with ignoreDifferentlyNamedParameters
:
- ❌ Incorrect
- ✅ Correct
function f(a: number): void;
function f(a: string): void;
Open in Playgroundfunction f(a: number): void;
function f(b: string): void;
Open in PlaygroundWhen Not To Use It
This is purely a stylistic rule to help with readability of function signature overloads. You can turn it off if you don't want to consistently keep them next to each other and unified.