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

turf-nearest-point: maintain point properties in result type #2866

Merged
merged 1 commit into from
Apr 8, 2025

Conversation

EmilJunker
Copy link
Contributor

@EmilJunker EmilJunker commented Apr 7, 2025

This PR improves the NearestPoint type and the type definition for the nearestPoint function.

Consider the following example in TypeScript:

const coordinatesList = [[0, 0], [1, 1], [0, 1]];
const pointsList = coordinatesList.map((coordinate, i) => {
    return turf.point(coordinate, {
        name: `point${i}`
    });
});
const pointsCollection = turf.featureCollection(pointsList);
const myNearestPoint = turf.nearestPoint(turf.point([2, 2]), pointsCollection);

With the old behaviour myNearestPoint.properties.name is of type any according to TypeScript.

With the changes in this PR, the type of myNearestPoint.properties.name is correctly inferred as string.

This change should be non-breaking.

This PR replaces #2861.

Please provide the following when creating a PR:

  • Meaningful title, including the name of the package being modified.
  • Summary of the changes.
  • Heads up if this is a breaking change.
  • Any issues this resolves.
  • Confirmation you've read the steps for preparing a pull request.

Copy link
Collaborator

@twelch twelch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed in light testing that user-provided properties are properly flowing through to the returned type, while also picking up featureIndex and distanceToPoint.

@twelch twelch changed the title Improve NearestPoint type for @turf/nearest-point so that it keeps the properties of the points in the FeatureCollection turf-nearest-point: maintain point properties in result type Apr 8, 2025
@twelch twelch merged commit 326a96a into Turfjs:master Apr 8, 2025
3 checks passed
@twelch
Copy link
Collaborator

twelch commented Apr 8, 2025

Thank you @EmilJunker for improving this. It will become available in the next release, timing TBD.

@EmilJunker EmilJunker deleted the nearest-point-properties-type branch April 9, 2025 08:03
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.

2 participants