nodejs-common icon indicating copy to clipboard operation
nodejs-common copied to clipboard

Improve ServiceObject types

Open callmehiphop opened this issue 6 years ago • 2 comments

👋

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.

callmehiphop avatar May 21 '19 18:05 callmehiphop

/cc @bcoe @JustinBeckwith @stephenplusplus

callmehiphop avatar May 21 '19 18:05 callmehiphop

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 with gaxios, and it's great 🙃

JustinBeckwith avatar May 21 '19 20:05 JustinBeckwith

Closing for now, feel free to reopen if this FR is still needed.

sofisl avatar Feb 02 '23 20:02 sofisl