Skip to content

Commit

Permalink
feat(ol/feature): infer type for feature.getProperties()
Browse files Browse the repository at this point in the history
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 #14868
  • Loading branch information
d-koppenhagen committed Jan 21, 2024
1 parent d6975d4 commit f1e81d8
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ol/render/Feature.js
Expand Up @@ -52,6 +52,7 @@ const tmpTransform = createTransform();
* Lightweight, read-only, {@link module:ol/Feature~Feature} and {@link module:ol/geom/Geometry~Geometry} like
* structure, optimized for vector tile rendering and styling. Geometry access
* through the API is limited to getting the type and extent of the geometry.
* @template {Object<string, *>} T Type of the properties object.
*/
class RenderFeature {
/**
Expand All @@ -60,7 +61,7 @@ class RenderFeature {
* to be right-handed for polygons.
* @param {Array<number>} ends Ends.
* @param {number} stride Stride.
* @param {Object<string, *>} properties Properties.
* @param {T} properties Properties.
* @param {number|string|undefined} id Feature id.
*/
constructor(type, flatCoordinates, ends, stride, properties, id) {
Expand Down Expand Up @@ -113,7 +114,7 @@ class RenderFeature {

/**
* @private
* @type {Object<string, *>}
* @type {T}
*/
this.properties_ = properties;

Expand Down Expand Up @@ -282,7 +283,7 @@ class RenderFeature {

/**
* Get the feature properties.
* @return {Object<string, *>} Feature properties.
* @return {T} Feature properties.
* @api
*/
getProperties() {
Expand Down

0 comments on commit f1e81d8

Please sign in to comment.