perron icon indicating copy to clipboard operation
perron copied to clipboard

Support generic type in ServiceClientResponse

Open cberg-zalando opened this issue 4 years ago • 4 comments

To avoid type clashes due to changes in perron's typings, I looked into bumping the version in translation-manager from 0.11.1 to 0.11.4. Due to the change from any to Buffer | string | object, this would now require adding type casts throughout the code.

Do you see any problem, if we change the API to extend ServiceClientResponse with a generic type, which can be then also passed via the ServiceClient::request method?

cberg-zalando avatar Apr 22 '20 07:04 cberg-zalando

We can add a generic type that default to the union. @cberg-zalando will you push a PR?

ruiaraujo avatar Apr 22 '20 07:04 ruiaraujo

Sure, I can do this. To be backwards compatible, it is just a question what kind of type to assign by default to the generic parameter. I would propose any, which would actually restore backwards comptability to the release before 0.11.4.

cberg-zalando avatar Apr 22 '20 08:04 cberg-zalando

I agree to keep the API compatible in 0.11.x. So, any as the default sounds good to me.

I also thought of adding more restriction like:

request<T extends (Buffer | string | object) = any>(options) : Promise<T>;

but it doesn't work as expected somehow.

Also, the parsed JSON type can be a boolean, number or an array in addition to string and object. So, we need a bit more consideration if we want a narrower type than any.

shuhei avatar Apr 22 '20 12:04 shuhei

I just gave this a revisit as I just didn't have any time to look into this issue before. I have a WIP commit, but I have run into a problem.

If add the generic type <P = any> to request(options: ServiceClientRequestOptions) method, I run into an issue that due to the change introduced in #105, TypeScript fails due to P and string | Buffer having nothing in common.

cberg-zalando avatar Jun 09 '20 05:06 cberg-zalando