Saltearse al contenido

useShorthandFunctionType

Esta página aún no está disponible en tu idioma.

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.

interface Example {
(): string;
}
code-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

function foo(example: { (): number }): number {
return example();
}
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 }

type Example = () => string;
function foo(example: () => number): number {
return bar();
}
// returns the function itself, not the `this` argument.
type ReturnsSelf2 = (arg: string) => ReturnsSelf;
interface Foo {
bar: string;
}
interface Bar extends Foo {
(): void;
}
// multiple call signatures (overloads) is allowed:
interface Overloaded {
(data: string): number;
(id: number): string;
}
// this is equivalent to Overloaded interface.
type Intersection = ((data: string) => number) & ((id: number) => string);
biome.json
{
"linter": {
"rules": {
"style": {
"useShorthandFunctionType": "error"
}
}
}
}