-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat(ol/feature): allow generics for feature.getProperties()
#15496
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! However, I'm failing to understand the benefit of this change. However, the result is no better than the type cast you initially had - you still have to make an assumption on what the return type is, and TypeScript won't help you checking it because of the @ts-ignore
.
I don't think there is a good easy alternative. A more involved alternative would be to define a generic PropertyType on the Feature constructor, and allow setting it also on VectorSource and FeatureFormat. If set on FeatureFormat, VectorSource should pick it up. Doable, but not easy.
📦 Preview the website for this branch here: https://deploy-preview-15496--ol-site.netlify.app/. |
with this change it's possible to infer the type `T` of passed properties when calling `feature.getProperties()` and using with TypeScript. ```ts type Vehicle = { type: 'Car' | 'Bike' | 'Truck', color: string } const car: Vehicle = { type: 'Car', color: 'Red' } // Before the change const feature = new RenderFeature(car); const properties = feature.getProperties(); // properties inferred as Object<string, *> // After the change const feature = new RenderFeature(car); const properties = feature.getProperties(); // properties inferred as Vehicle ``` closes openlayers#14868
4355a97
to
f1e81d8
Compare
@ahocevar you were totally right. I was on the wrong path I think. Please review again. Now it should be possible to infer the type. |
feature.getProperties<T>()
feature.getProperties()
@d-koppenhagen Are you able to benefit from this? With your last changes, it looks like you only would if you created |
I believe the get function should also be typed accordingly. Something like this should do the trick.
|
with this change it's possible to pass a generic type
T
when callingfeature.getProperties<T>()
and using with TypeScript.closes #14868