Skip to main content

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.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/unified-signatures": "error"
}
});

Try this rule in the playground ↗

Examples

function x(x: number): void;
function x(x: string): void;
Open in Playground
function y(): void;
function y(...x: number[]): void;
Open in Playground

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;
/** Whether two overloads with different JSDoc comments should be considered different even if their parameter and return types are the same. */
ignoreOverloadsWithDifferentJSDoc?: boolean;
},
];

const defaultOptions: Options = [
{
ignoreDifferentlyNamedParameters: false,
ignoreOverloadsWithDifferentJSDoc: 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:

function f(a: number): void;
function f(a: string): void;
Open in Playground

ignoreOverloadsWithDifferentJSDoc

Whether two overloads with different JSDoc comments should be considered different even if their parameter and return types are the same. Default: false.

Examples of code for this rule with ignoreOverloadsWithDifferentJSDoc:

declare function f(x: string): void;
declare function f(x: boolean): void;
/**
* @deprecate
*/
declare function f(x: number): void;
/**
* @deprecate
*/
declare function f(x: null): void;
Open in Playground

When 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.

Resources