useUnifiedTypeSignature
Este conteúdo não está disponível em sua língua ainda.
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useUnifiedTypeSignature
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
Description
Section titled “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
Section titled “Examples”Invalid
Section titled “Invalid”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 {}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useUnifiedTypeSignature": "error" } } }}