-
Notifications
You must be signed in to change notification settings - Fork 991
Improve type inference in @turf/helpers' geometry and geometryCollection #2971
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,30 +207,39 @@ export function feature< | |
| * var geometry = turf.geometry(type, coordinates); | ||
| * // => geometry | ||
| */ | ||
| export function geometry( | ||
| type: | ||
| export function geometry< | ||
| T extends | ||
| | "Point" | ||
| | "LineString" | ||
| | "Polygon" | ||
| | "MultiPoint" | ||
| | "MultiLineString" | ||
| | "MultiPolygon", | ||
| >( | ||
| type: T, | ||
| coordinates: any[], | ||
| _options: Record<string, never> = {} | ||
| ) { | ||
| ): { | ||
| Point: Point; | ||
| LineString: LineString; | ||
| Polygon: Polygon; | ||
| MultiPoint: MultiPoint; | ||
| MultiLineString: MultiLineString; | ||
| MultiPolygon: MultiPolygon; | ||
| }[T] { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had considered making this a named interface and mapping against that instead ( |
||
| switch (type) { | ||
| case "Point": | ||
| return point(coordinates).geometry; | ||
| return point(coordinates).geometry as any; | ||
| case "LineString": | ||
| return lineString(coordinates).geometry; | ||
| return lineString(coordinates).geometry as any; | ||
| case "Polygon": | ||
| return polygon(coordinates).geometry; | ||
| return polygon(coordinates).geometry as any; | ||
| case "MultiPoint": | ||
| return multiPoint(coordinates).geometry; | ||
| return multiPoint(coordinates).geometry as any; | ||
| case "MultiLineString": | ||
| return multiLineString(coordinates).geometry; | ||
| return multiLineString(coordinates).geometry as any; | ||
| case "MultiPolygon": | ||
| return multiPolygon(coordinates).geometry; | ||
| return multiPolygon(coordinates).geometry as any; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| default: | ||
| throw new Error(type + " is invalid"); | ||
| } | ||
|
|
@@ -598,15 +607,20 @@ export function multiPolygon<P extends GeoJsonProperties = GeoJsonProperties>( | |
| * // => collection | ||
| */ | ||
| export function geometryCollection< | ||
| G extends | ||
| | Point | ||
| | LineString | ||
| | Polygon | ||
| | MultiPoint | ||
| | MultiLineString | ||
| | MultiPolygon, | ||
| P extends GeoJsonProperties = GeoJsonProperties, | ||
| >( | ||
| geometries: Array< | ||
| Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | ||
| >, | ||
| geometries: Array<G>, | ||
| properties?: P, | ||
| options: { bbox?: BBox; id?: Id } = {} | ||
| ): Feature<GeometryCollection, P> { | ||
| const geom: GeometryCollection = { | ||
| ): Feature<GeometryCollection<G>, P> { | ||
| const geom: GeometryCollection<G> = { | ||
| type: "GeometryCollection", | ||
| geometries, | ||
| }; | ||
|
|
||
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.
In a simliar fashion, we could narrow this
any[]based onT, but that would be a breaking change, we can file it against the 8.0 milestone. I suppose you could also just make the case that we should do this in 7.x, because it is explicitly a bug in the caller if they aren't passing in the correctly shaped coordinates. My only concern with that is some amount of weirdness withnumber[]vs[number, number]issues, which might crop up depending on how consumers wrote their own types.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.
The [number, number] thing is going to haunt us forever 😅