当我使用 Typescript 投射内容时,为什么 Prettier 添加了这个分号?

Fre*_*ors 5 typescript prettier

我正在使用 Typescript 和 Prettier,每次我在函数 Prettier 的第一行中投射一些内容时,都会将分号作为第一个字符。

为什么?看来没有必要。我错了吗?

我读了基本原理,但仍然不明白为什么我们需要在那里放第一个分号。

Prettier 2.2.1 Playground 链接

--parser typescript
--no-semi
Run Code Online (Sandbox Code Playgroud)

输入:

function example() {
(this as MyType).method() // useless semicolon will be placed here
;(f as any).y = fy // the semicolon here is ok
return f
}
Run Code Online (Sandbox Code Playgroud)

输出:

function example() {
  ;(this as MyType).method() // useless semicolon here
  ;(f as any).y = fy // the semicolon here is ok
  return f
}

Run Code Online (Sandbox Code Playgroud)

预期行为:

不得放置第一个分号。

在 Prettier 存储库上提出了一个问题,但他们建议我在这里提问。

***更新:

如果我使用更漂亮的默认值,我也会遇到问题,请参阅此示例:

Prettier 2.2.1 Playground 链接

function example() {
(this as MyType).method() // useless semicolon will be placed here
;(f as any).y = fy // the semicolon here is ok
return f
}
Run Code Online (Sandbox Code Playgroud)

输入:

export function inputSelect(e: MouseEvent): void {
    console.log("Test this")
    (e.target as HTMLInputElement)?.select();
}
Run Code Online (Sandbox Code Playgroud)

输出:

export function inputSelect(e: MouseEvent): void {
  console
    .log("Test this")(e.target as HTMLInputElement)
    ?.select();
}

Run Code Online (Sandbox Code Playgroud)

预期行为:

它应该将分号放在 的末尾console.log()