microsoft / microsoft/TypeScript
Code generated for optional call could be optimized when the result is unused
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the TypeScript compiler code that transforms optional chaining, using the issue's examples as the baseline for current and desired JavaScript output. Verify that unused optional-call results produce the shorter short-circuiting form while preserving optional-chain behavior, then add or update compiler tests for the shown cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers, performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100