AngelaE / ts-mountebank

Mountebank client for TS / node
15 stars 5 forks source link

Return this instead of class in builder methods #23

Closed TomLingham closed 1 year ago

TomLingham commented 1 year ago

I don't know if this is something that you're interested in accepting but figured I'd submit this and ask. This can be worked around as a consumer with some type casting 🙂

Currently, the class methods exported from classes ts-mountebank that use the builder patter (with<property>(...)), are typed as returning a hardcoded class type — this prevents consumers from extending the classes in an ergonomic way.

In the below example, you can see that the first image (current behaviour), after calling withName(...) on the instance of AutoPortImposter, typescript sees the return type as being the type of the parent class, where it's actually an instance of the subclass (or this). This PR modifies the return type to be this which allows extending classes defined in this library and using the builder pattern as expected.

Before this PR:

image

After this PR:

Screenshot 2023-07-11 at 7 16 35 am
AngelaE commented 1 year ago

Thanks!