Skip to content

Latest commit

 

History

History
81 lines (64 loc) · 1.58 KB

prefer-function-type.md

File metadata and controls

81 lines (64 loc) · 1.58 KB

Use function types instead of interfaces with call signatures (prefer-function-type)

Rule Details

This rule suggests using a function type instead of an interface or object type literal with a single call signature.

Examples of incorrect code for this rule:

interface Foo {
  (): string;
}
function foo(bar: { (): number }): number {
  return bar();
}
interface Foo extends Function {
  (): void;
}
interface MixinMethod {
  // returns the function itself, not the `this` argument.
  (arg: string): this;
}

Examples of correct code for this rule:

interface Foo {
  (): void;
  bar: number;
}
function foo(bar: { (): string; baz: number }): string {
  return bar();
}
interface Foo {
  bar: string;
}
interface Bar extends Foo {
  (): void;
}
// returns the `this` argument of function, retaining it's type.
type MixinMethod = <TSelf>(this: TSelf, arg: string) => TSelf;
// a function that returns itself is much clearer in this form.
type ReturnsSelf = (arg: string) => ReturnsSelf;
// multiple call signatures (overloads) is allowed:
interface Overloaded {
  (data: string): number;
  (id: number): string;
}
// this is equivelent to Overloaded interface.
type Intersection = ((data: string) => number) & ((id: number) => string);

When Not To Use It

If you specifically want to use an interface or type literal with a single call signature for stylistic reasons, you can disable this rule.

Further Reading