Microsoft/TypeScript

Code generated for optional call could be optimized when the result is unused

開放

#38,835 建立於 2020年5月28日

 (3 則留言) (0 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
Effort: ModerateExperience EnhancementHelp WantedSuggestion

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

The generated code for optional chaining is a little verbose when the "return" value of the expression is not being used. For example, when simply calling a function:

maybe.close?.()

Here is a more complete example.

The second example generates:

(_c = (_b = perhaps.maybe) === null || _b === void 0 ? void 0 : _b.close) === null || _c === void 0 ? void 0 : _c.call(_b);

But could be optimized to:

(_b = perhaps.maybe) !== null && _b !== void 0 && (_c = _b.close) !== null && _c !== void 0 && _c.call(_b);

It's not a huge difference, but it saves 8 characters for each link in the chain.

While the spec says something like: "the expression short-circuits with a return value of undefined", when the return value is meaningless, then only the "short-circuit" part is important.

貢獻者指南