useShorthandFunctionType
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/style/useShorthandFunctionType
Since: v1.5.0
Sources:
Enforce using function types instead of object type with call signatures.
TypeScript allows for two common ways to declare a type for a function:
- Function type:
() => string
- Object type with a signature:
{ (): string }
The function type form is generally preferred when possible for being more succinct.
This rule suggests using a function type instead of an interface or object type literal with a single call signature.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.ts:2:3 lint/style/useShorthandFunctionType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use a function type instead of a call signature.
1 │ interface Example {
> 2 │ (): string;
│ ^^^^^^^^^^^
3 │ }
4 │
ℹ Types containing only a call signature can be shortened to a function type.
ℹ Safe fix: Alias a function type instead of using an interface with a call signature.
1 │ - interface·Example·{
2 │ - ··():·string;
3 │ - }
1 │ + type·Example·=·()·=>·string
4 2 │
code-block.ts:1:25 lint/style/useShorthandFunctionType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use a function type instead of a call signature.
> 1 │ function foo(example: { (): number }): number {
│ ^^^^^^^^^^
2 │ return example();
3 │ }
ℹ Types containing only a call signature can be shortened to a function type.
ℹ Safe fix: Use a function type instead of an object type with a call signature.
1 │ - function·foo(example:·{·():·number·}):·number·{
1 │ + function·foo(example:·()·=>·number):·number·{
2 2 │ return example();
3 3 │ }