Skip to content

Latest commit

 

History

History
82 lines (55 loc) · 1.81 KB

prefer-property-signatures.md

File metadata and controls

82 lines (55 loc) · 1.81 KB

Prefer property signatures over method signatures (functional/prefer-property-signatures)

💼🚫 This rule is enabled in the 🎨 stylistic config. This rule is disabled in the disableTypeChecked config.

💭 This rule requires type information.

Rule Details

There are two ways function members can be declared in interfaces and type aliases; MethodSignature and PropertySignature.

The MethodSignature and the PropertySignature forms seem equivalent, but only the PropertySignature form can have a readonly modifier. Because of this any MethodSignature will be mutable unless wrapped in the Readonly type.

It should be noted however that the PropertySignature form does not support overloading.

❌ Incorrect

/* eslint functional/prefer-property-signatures: "error" */

type Foo = {
  bar(): string;
};

✅ Correct

/* eslint functional/prefer-property-signatures: "error" */

type Foo = {
  bar: () => string;
};

type Foo = {
  readonly bar: () => string;
};

Options

This rule accepts an options object of the following type:

type Options = {
  ignoreIfReadonlyWrapped: boolean;
};

Default Options

const defaults = {
  ignoreIfReadonlyWrapped: false,
};

ignoreIfReadonlyWrapped

If set to true, method signatures wrapped in the Readonly type will not be flagged as violations.

✅ Correct

/* eslint functional/prefer-property-signatures: ["error", { "ignoreIfReadonlyWrapped": true } ] */

type Foo = Readonly<{
  bar(): string;
}>;