microsoft / microsoft/TypeScript

[Bug Fix]: Bring parity to JS declaration generation for classes with static private members to match TS declaration output.

Open
#60,744 2 comments 1 reaction 1 assignee View on GitHub

@weswigham is already working on this.

Since Dec 12, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Acknowledgement
  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
  • TS versions: All recent (5.x)

Problem:
When generating type declarations for ESM / Javascript classes with private static members including properties and methods the code path for JS declaration generation does not take into account the private / # state of the symbol or associated declaration when collecting static members. This results in those class members becoming publicly visible in the generated declarations and furthermore with the mangled internal names assigned to private declarations in the binding process.

You can see the problem in this TS playground example. Notice the difference in the output .d.ts when switching between Javascript and Typescript via the TS Config options. The JS declaration generation path does not filter out static private class members nor adds the #private identifier based on static private members.

Please see this brief overview video showing the current results on TS 5.7.2:
https://github.com/user-attachments/assets/85e558fa-58e9-4192-b3ed-16c749e78b46

Expected Result:
That static private members of a class are removed from generated public type declarations in the same exact way for JS & TS source files. This is also valuable because this allows import elision to occur for any type references associated with static private members that may otherwise not be referenced in the public declarations.

Fix:
I have added the appropriate filtering step in src/compiler/checker.ts -> serializeAsClass to remove private static members from passing through to the output declarations. I have ensured that the #private identifier is correctly added for classes that only have private static members. I have added a test case and baseline reference. You can view the code changes here.

PR:
Upon review and acceptance of this bug & my proposed fix I can provide a timely submission of a PR.

Future:
I am interested in fixing more JSDoc / Javascript / declaration generation issues improving support. I'm familiar with TS AST and the TS code area of impact in the declaration generation pipeline.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.