箭头函数隐式返回用括号自动包装

Ita*_*tai 4 typescript arrow-functions visual-studio-code angular prettier

我正在使用带有 Prettier 的 Visual Studio Code,其功能如下:

(token: string) => this.token = token
Run Code Online (Sandbox Code Playgroud)

变成:

(token: string) => (this.token = token)
Run Code Online (Sandbox Code Playgroud)

我认为它使它不那么可读......有没有办法防止这种情况?

bug*_*lot 5

这是由于不返回分配规则。请参阅https://eslint.org/docs/rules/no-return-assign

不管你怎么想,你的箭头函数等价于

(token: string) => {return this.token = token}
Run Code Online (Sandbox Code Playgroud)

是的,那里有回报,并且由于分配而变得“美化”。

此规则仅有的两个选项是在出现括号时允许,或始终禁止。

因此,要解决您的“可读性问题”,请使用花括号,或尝试禁用该规则(不推荐)。