microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.11k stars 12.37k forks source link

Add support for beforeDeclarations #58880

Open Danielku15 opened 2 months ago

Danielku15 commented 2 months ago

🔍 Search Terms

beforeDeclarations declaration transform transformers afterDeclarations

✅ Viability Checklist

⭐ Suggestion

The TypeScript compiler API already supports custom transformers in 3 ways:

  1. Before built-in .js transformations
  2. After built-in .js transformations
  3. After built-in .d.ts transformations

But the API is missing a "Before built-in .d.ts transformations" variant.

📃 Motivating Example

To generate type declarations more dynamically, you often need to access AST nodes of your codebase as it is written. The built-in transformers erase already a lot of the AST making this information unavailable. With the help of this new beforeDeclarations you are able to access your full AST and use it to auto-generate your typescript declaration files.

💻 Use Cases

  1. Use information from property initializers to include "default values" your JSDoc comments. (see #41486).
  2. Remove declarations using information like visibility modifiers or used functions in your code to not expose implementation details.
Danielku15 commented 2 months ago

Referencing some more related issues where a beforeDeclaration hooks would have a benefit:

The afterDeclarations was added in

And another problem: