typescript/prefer-function-type Style
它的作用
强制使用函数类型,而不是带有调用签名的接口。
这为什么不好?
TypeScript 允许用两种常见方式来声明函数类型:
- 函数类型:
() => string - 带有签名的对象类型:
{ (): string }
在可能的情况下,通常更推荐使用函数类型,因为它更简洁、更易读。只有调用签名的接口会增加不必要的冗长,而不会提供额外功能。
示例
以下是此规则判定为错误的代码示例:
typescript
interface Example {
(): string;
}
function foo(example: { (): number }): number {
return example();
}
interface ReturnsSelf {
(arg: string): this;
}以下是此规则判定为正确的代码示例:
typescript
type Example = () => string;
function foo(example: () => number): number {
return example();
}
// 返回函数本身,而不是 `this` 参数
type ReturnsSelf = (arg: string) => ReturnsSelf;
// 允许多个属性
function foo(bar: { (): string; baz: number }): string {
return bar();
}
// 允许多个调用签名(重载)
interface Overloaded {
(data: string): number;
(id: number): string;
}
// 这等价于 Overloaded 接口。
type Intersection = ((data: string) => number) & ((id: number) => string);如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/prefer-function-type": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/prefer-function-type": "error",
},
});bash
oxlint --deny typescript/prefer-function-type版本
此规则在 v0.2.11 中新增。
