dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5k stars 195 forks source link

Feature request: "Type alias to stub class" API #1304

Open ajvincent opened 2 years ago

ajvincent commented 2 years ago

TypeScript, for all its power, does not give us a simple way to take a complex type (mapped type with additional arguments, for example) and generate a stub class. Thanks to this project, I've been able to implement a "type to stub class" API.

I'm just nuts enough to think this would be very useful to have in the toolbox.

Describe the solution you'd like

I've tried to write this to be general-purpose. At the very least, I'd appreciate a review of the code, even if it's turned down for inclusion in this project. I'd like to see if ts-morph would be willing to adopt it as part of its standard API.

I am willing to contribute this code. That said, I did have a recent incident where I was over-enthusiastic about a particular feature I wanted to add to another project, with other parties also interested. That enthusiasm blew up in my face on an inter-personal level with the other developers. I'd very much like to not repeat that mistake. I also recognize that other people have limited time to offer.

Describe alternatives you've considered

I have not seen this particular capability in the open-source community. Just getting the list of properties a type specifies is difficult.

I first tried to do this using TypeScript-ESLint's parser. As I approached the more complex types, I realized I might end up re-implementing a lot of TypeScript's type checking. This led me to the Discord channel for TypeScript's internal API's, which led me to this project and ultimately the solution I came up with.

ajvincent commented 1 year ago

Okay, I'm rewriting this to use structures instead. Mapped types are at present giving me lots of trouble - and they're the reason I started using ts-morph in the first place.