Improve ServiceObject types
👋
I'm implementing a new feature that uses the ServiceObject class and have found myself getting frustrated with some of the typings. I have a few thoughts on how we could improve the types here and would love to get some yoshisaur opinions.
Use this instead of a generic type variable
The ServiceObject accepts a generic type variable that is used to pass around a reference to whatever the type that extends it is. TypeScript actually supports a this type, which seems more appropriate in this case.
Use a generic type variable for metadata
Every ServiceObject instance has a metadata property that is just the JSON representation of a response body. Right now it is set as an alias to any, which gives our users no type safety when accessing members on it. Using a generic here makes sense to me.
Allow for a generic type variable in the request() function
I think it would be nice if we didn't have to explicitly set types in request callbacks, I think adding a generic type variable to the request method would be a great fix here.
/cc @bcoe @JustinBeckwith @stephenplusplus
Initial thoughts:
- Use this instead of a generic type variable - yep, go for it!
- Use a generic type variable for metadata - This should be optional so it's not a breaking change, but I'm otherwise fine with it. Would be good to see an example.
- Allow for a generic type variable in the request() function - same deal as before - this should default to
any, but let you override it. We do this withgaxios, and it's great 🙃
Closing for now, feel free to reopen if this FR is still needed.