下面的代码不使用类或this
. 如何防止仅函数(无类)代码中的 typescript-eslint 错误?我知道我可以全局禁用该规则,但该规则对类代码很有用。我知道我可以一次禁用它一行,但考虑到在 TS 接口中定义回调函数是多么普遍,这似乎很痛苦。还有其他方法吗?
这是问题的简化示例:
interface FooProps {
bar(): void;
}
export function foo(props: FooProps) {
const { bar } = props;
// ^^^ Avoid referencing unbound methods which may cause unintentional scoping of `this`.
// eslint(@typescript-eslint/unbound-method)
return bar;
}