-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Remove readonly
modifier from all SearchOptions
properties (and most likely other types)
#1375
Comments
hey, thanks for the very thorough proposal. We are planning a next major for this client, and those types will most likely be gone. (No ETA) |
Is there anything I can help with speeding up this process? Or is this part of a major overhaul as part of a new major release? |
I actually think removing readonly might not be a major version, as it's less restrictive: https://www.typescriptlang.org/play?#code/C4TwDgpgBAhgjFAvFA3gWAFBW1AThGAEwHsA7AGxCgCMAuKAZ2FwEtSBzTAX001ElgAmJKkw4a9Jqw7deGAGYBXUgGNgLMlHlwAFDFzt68AJSoeC5Wo2ktgvQaODTKc5m06UdKAHJvXY252nvS+-nLuwT5+sAxQKmRMAQpBXqExcQnASZjxpEywBnBGCMiRoeG6+uxwSfJ2VTVyuflVgo4iZX4V9uxOgT1OQA Not sure if I'm missing anything though, maybe you can't cast or assume something is readonly anymore? |
I'm not sure either, though I'd believe that it'd only really affect Algolia's internals if some new method code hasn't been updated to At the end of the day I'm fine with either option: Major or minor version. That's totally up to you :) |
I'd like to know if there is any progress on this issue? I have to agree with @keichinger this is really cumbersome in many scenarios. |
Hey @unwobbling, readonly have been removed in v5 (https://github.com/algolia/algoliasearch-client-javascript/tree/next), you can track development on https://github.com/algolia/api-clients-automation |
The problem
Currently, most of the types (if not all?) are marked with
readonly
all over the place. In some types it totally makes sense, though in some types it's just super inconvenient to have the property marked asreadonly
. One example is theSearchOptions
type, which is used in things likeSearchIndex
'ssearch
method for therequestOptions
argument:Now imagine having an application, that conditionally needs to add more properties to the
requestOptions
argument, depending on some internal application logic, which could look roughly like this:This is currently strictly forbidden as
SearchOptions
explicitly says that the property isreadonly
, thus it cannot be assigned after its initial creation.Now I have a couple of options, which all are equally a hack and come with potential side effects, that I'm not very fond of:
Option 1: Remove the
readonly
modifier within my app codeWe could simply remove the
readonly
modifier from the type like so:The potential downside is that we'd make it appear that some property, which really should be
readonly
, notreadonly
and can be assigned. It's also not very forward stable.Option 2: Don't use the Algolia types
It sucks for multiple reasons: I'm not getting any auto-completion, real type and property checking and it's not forward stable.
Option 3: Remove the
readonly
from the type itself and useReadonly<SearchOptions>
where necessary within AlgoliaThe updated type definitions for
SearchIndex
would look like this:Conclusion/Proposal
By far the best option is number 3 because I as a library consumer will get all of TypeScript's benefits and the only downside would be the amount of work necessary to refactor internal (and potentially external) APIs to use
Readonly<SearchOptions>
where really necessary.This moves the readonly-check away from the assignment into the function and does exactly what you want: Not accidentally modify your input arguments.
A slightly dumbed down example can be seen here:
readonly
properties: https://www.typescriptlang.org/play?#code/C4TwDgpgBAyhCGAnAxgCwPJmASwPYDsBnKAXigG8AoKGqRBAEwIBsQp4B+ALikOEWz4A5gG5qtevCb5WUAEbde-QaMoBfMZQBmAV3zIcBKFty45SKAAoG8YPB5wkaTIaIBKKDwBuubA0pUtFA2dgB08KRQAETwUWJBIfChcpFRcnHqlJTMEMDGpg4IKBhYeESRgbT20Sa4UQA0mZTYWlb8OhBuAeI0tcmp5ogZalm1g5a1biJAAReadonly<T>
arguments: https://www.typescriptlang.org/play?#code/C4TwDgpgBAyhCGAnAxgCwPJmASwPYDsBnKAXigG8AoKGqeAfgC4pDhFt8BzAbmtoCMmLNhx6UAvr0oAzAK75kOAlGm5c-JFAAUAE3jB4zAEoIdBADYgAPHCRpMSogD4AlFGYA3XNh2UqtKD0DADp4UigAIngI3gCg+GD+cIj+GIlKSnMIYBU1ZlsUDCw8InD-WkNI1VwIgBp0ymxpbTZZCBc-PhpqxOSNRDTxDOr+rWqXbiAWhat do you guys think?
/cc @Haroenv
The text was updated successfully, but these errors were encountered: