Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

d-koppenhagen
Copy link

with this change it's possible to pass a generic type T when calling feature.getProperties<T>() and using with TypeScript.

// before: const stop = feature?.getProperties() as Stop
const stop = feature?.getProperties<Stop>()
console.log(typeof stop) // "Stop"

closes #14868

Copy link
Member

@ahocevar ahocevar left a 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.

Copy link

📦 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
@d-koppenhagen
Copy link
Author

@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.

@d-koppenhagen d-koppenhagen changed the title feat(ol/feature): allow generics for feature.getProperties<T>() feat(ol/feature): allow generics for feature.getProperties() Jan 23, 2024
@ahocevar
Copy link
Member

@d-koppenhagen Are you able to benefit from this? With your last changes, it looks like you only would if you created RenderFeature instances manually, instead of retrieving them from a FeatureFormat or a VectorTile source.

@Guillaumedemoff
Copy link

I believe the get function should also be typed accordingly.

Something like this should do the trick.

  /**
   * Get a feature property by its key.
   * @template {keyof T} Key
   * @param {Key} key Key
   * @return {T[Key]} Value for the requested key.
   * @api
   */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TS allow generics for getProperties()
3 participants