method-signature-style
强制使用特定方法签名语法.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
TypeScript 提供了两种定义对象/接口函数属性的方法:
¥TypeScript provides two ways to define an object/interface function property:
interface Example {
// method shorthand syntax
func(arg: string): number;
// regular property with function type
func: (arg: string) => number;
}
两者非常相似;大多数时候,使用哪一个并不重要。
¥The two are very similar; most of the time it doesn't matter which one you use.
一种好的做法是使用 TypeScript 的 strict
选项(这意味着 strictFunctionTypes
),它仅对函数属性启用正确的类型检查(方法签名会获得旧行为)。
¥A good practice is to use the TypeScript's strict
option (which implies strictFunctionTypes
) which enables correct typechecking for function properties only (method signatures get old behavior).
TypeScript 常见问题解答:
¥TypeScript FAQ:
同一类型的方法和函数属性的行为不同。在
strictFunctionTypes
下,方法的参数始终是双变的,而函数属性的参数是逆变的。¥A method and a function property of the same type behave differently. Methods are always bivariant in their argument, while function properties are contravariant in their argument under
strictFunctionTypes
.
有关背后的原因,请参阅 编译器选项的 TypeScript PR。
¥See the reasoning behind that in the TypeScript PR for the compiler option.
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/method-signature-style": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/method-signature-style": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
type Options = ['method' | 'property'];
const defaultOptions: Options = ['property'];
¥Options
此规则接受一个字符串选项:
¥This rule accepts one string option:
-
"property"
:强制使用函数的属性签名。使用它与 TypeScript 的严格模式一起强制执行最大正确性。¥
"property"
: Enforce using property signature for functions. Use this to enforce maximum correctness together with TypeScript's strict mode. -
"method"
:强制使用函数的方法签名。如果你不使用 TypeScript 的严格模式并且更喜欢这种风格,请使用此选项。¥
"method"
: Enforce using method signature for functions. Use this if you aren't using TypeScript's strict mode and prefer this style.
property
带有 property
选项的代码示例。
¥Examples of code with property
option.
- ❌ 错误
- ✅ 正确
interface T1 {
func(arg: string): number;
}
type T2 = {
func(arg: boolean): void;
};
interface T3 {
func(arg: number): void;
func(arg: string): void;
func(arg: boolean): void;
}
Open in Playgroundinterface T1 {
func: (arg: string) => number;
}
type T2 = {
func: (arg: boolean) => void;
};
// this is equivalent to the overload
interface T3 {
func: ((arg: number) => void) &
((arg: string) => void) &
((arg: boolean) => void);
}
Open in Playgroundmethod
带有 method
选项的代码示例。
¥Examples of code with method
option.
- ❌ 错误
- ✅ 正确
interface T1 {
func: (arg: string) => number;
}
type T2 = {
func: (arg: boolean) => void;
};
Open in Playgroundinterface T1 {
func(arg: string): number;
}
type T2 = {
func(arg: boolean): void;
};
Open in Playground何时不使用它
¥When Not To Use It
如果你不想强制执行对象/接口函数类型的特定样式,和/或如果你不使用 strictFunctionTypes
,那么你不需要此规则。
¥If you don't want to enforce a particular style for object/interface function types, and/or if you don't use strictFunctionTypes
, then you don't need this rule.
但是,请记住,不一致的风格可能会损害项目的可读性。我们建议为此规则选择一个最适合你的项目的选项。
¥However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.