Skip to content

useShorthandFunctionType (since v1.5.0)

Diagnostic Category: lint/style/useShorthandFunctionType

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.

interface Example {
(): string;
}
style/useShorthandFunctionType.js: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();
}
style/useShorthandFunctionType.js: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);