Skip to content

useUnifiedTypeSignatures

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useUnifiedTypeSignatures": "error"
}
}
}
}

¥Description

禁止使用可以合并为单个签名的重载签名。

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

¥Examples

¥Invalid

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

ℹ Multiple similar 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/style/useUnifiedTypeSignatures  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Multiple similar 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 │

¥Valid

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 {}

¥Related links