microsoft / microsoft/TypeScript
Unneeded temporary introduced for named class expression with static property initializer
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
classes, static properties, temporary variable
Suggestion
When a class expression is returned from a function, it gets assigned to a temporary variable that is not needed there.
Use Cases
Less verbose and more clear JavaScript output.
Examples
Input:
const f = () => class { static x = 1; };
const g = () => class _ { static x = 1; };
const h = () => { class _ { static x = 1; } return _; };
Current output (3.7.2) (formatted a little differently):
const f = () => { var _a; return _a = class { }, _a.x = 1, _a; };
const g = () => { var _a; return _a = class _ { }, _a.x = 1, _a; };
const h = () => { class _ { }; _.x = 1; return _; };
Desired output:
const f = () => { var _a = class { }; _a.x = 1; return _a; };
const g = () => { class _ { } _.x = 1; return _; };
const h = () => { class _ { } _.x = 1; return _; };
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 reproducing the three TypeScript examples and comparing the current JavaScript output with the desired output. Trace how named and anonymous class expressions with static property initializers are emitted, then verify that the generated output avoids the unnecessary temporary while preserving runtime behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100