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

TS allow generics for getProperties() #14868

Open
d-koppenhagen opened this issue Jun 28, 2023 · 0 comments · May be fixed by #15496
Open

TS allow generics for getProperties() #14868

d-koppenhagen opened this issue Jun 28, 2023 · 0 comments · May be fixed by #15496

Comments

@d-koppenhagen
Copy link

Is your feature request related to a problem? Please describe.
As a user of Openlayers with TypeScript, I want to be able to receive feature properties by passing a type as generic.
This allows to explicitly set the type of feature properties for further processing.

Describe the solution you'd like

Current Behavior:

const stop = feature?.getProperties() as Stop

Solution i'd like:

const stop = feature?.getProperties<Stop>()
d-koppenhagen added a commit to d-koppenhagen/openlayers that referenced this issue Jan 21, 2024
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 openlayers#14868
d-koppenhagen added a commit to d-koppenhagen/openlayers that referenced this issue Jan 21, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant