Pular para o conteúdo

useUnifiedTypeSignature

Este conteúdo não está disponível em sua língua ainda.

Disallow overload signatures that can be unified into a single signature.

Overload signatures that can be merged into a single signature are redundant and should be avoided. This rule helps simplify function signatures by combining overloads by making parameters optional and/or using type unions.

function f(a: number): void;
function f(a: string): void;
code-block.ts:1:1 lint/nursery/useUnifiedTypeSignature  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Overload signatures are hard to read and maintain.

> 1 │ function f(a: number): void;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ function f(a: string): void;
3 │

Unsafe fix: Combine overloads using a type union.

1 - function·f(a:·number):·void;
2 - function·f(a:·string):·void;
1+
2+ function·f(a:·string·|·number):·void;
3 3

interface I {
a(): void;
a(x: number): void;
}
code-block.ts:2:5 lint/nursery/useUnifiedTypeSignature  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Overload signatures are hard to read and maintain.

1 │ interface I {
> 2 │ a(): void;
^^^^^^^^^^
3 │ a(x: number): void;
4 │ }

Unsafe fix: Combine overloads by making parameters optional.

1 1 interface I {
2 - ····a():·void;
3 - ····a(x:·number):·void;
2+ ····a(x?:·number):·void;
4 3 }
5 4

function f(a: number | string): void {}
interface I {
a(x?: number): void;
}

Different return types cannot be merged:

interface I {
f(): void;
f(x: number): number;
}

Different type parameters cannot be merged:

function f<T extends number>(x: T): void;
function f<T extends string>(x: T): void;
function f(x: unknown): void {}
biome.json
{
"linter": {
"rules": {
"nursery": {
"useUnifiedTypeSignature": "error"
}
}
}
}