algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

ReadOnly in types

Open dispix opened this issue 4 years ago • 6 comments

Hi,

I started using the v4 and had some type issues with the transporter:

https://github.com/algolia/algoliasearch-client-javascript/blob/97dbe4e5f4259d26670c6abb3527aa276f59513f/packages/transporter/src/types/Transporter.ts#L78-L93

read and write return a ReadOnly<Promise<TResponse>> which is a nice intent but makes it incompatible with function or whatever else that expects a Promise<TResponse>. How would you feel about removing the ReadOnly, which may make slightly less type safe but much more compatible with other libraries/custom code?

dispix avatar Apr 21 '20 08:04 dispix

What if instead TResponse would be the readonly, I think that could help

Haroenv avatar Apr 21 '20 09:04 Haroenv

What if instead TResponse would be the readonly, I think that could help

That seem to work in my specific use case but I'm not sure it wouldn't create type issues in some cases. Plus, do you really want to prevent the consumer from applying write operations on the Payload?

dispix avatar Apr 21 '20 09:04 dispix

Plus, do you really want to prevent the consumer from applying write operations on the Payload?

No. And since v4.2.0, all the responses are now writable: https://github.com/algolia/algoliasearch-client-javascript/pull/1068.

read and write return a ReadOnly<Promise<TResponse>> which is a nice intent but makes it incompatible with function or whatever else that expects a Promise<TResponse>.

I need to think about this a little bit,

nunomaduro avatar Apr 21 '20 09:04 nunomaduro

I'm currently upgrading from 3.35.1 to 4.3.0 - I used to do this

 await usersIndex.addObject({ ... });

after changing to

 await usersIndex.saveObject({ ... });

I'm getting the a Typescript warning saying Invalid 'await' of a non-Promise value. (await-promise).

~Also im not too sure when addObject changed to saveObject but it wasn't in the migration guide~

malimccalla avatar Jun 13 '20 10:06 malimccalla

@malimccalla I wasnt so sure if the strikethrough was because you found in migration guide, but it is here

mateusppereira avatar Jul 02 '20 18:07 mateusppereira

Malim had the right link at the time, but I updated it to add addObject -> saveObject migration (it was mentioned somewhere else, but not in the real migration guide for some reason) :)

Haroenv avatar Jul 08 '20 08:07 Haroenv