From b2df1538f283efa702c71c38d57f9176eb79c51c Mon Sep 17 00:00:00 2001
From: Matthew Fedderly <24275386+mfedderly@users.noreply.github.com>
Date: Sun, 14 Dec 2025 19:44:31 -0500
Subject: [PATCH] Increase strictness of @turf/line-chunk input
The method only works correctly for LineString and MultiLineString shaped geometries (with any containing Features, Collections, etc.
This makes the strictness more explicit in the docs, the TypeScript types, and runtime checking.
---
packages/turf-helpers/index.ts | 15 ++++++---
packages/turf-line-chunk/index.ts | 12 ++++++--
packages/turf-line-chunk/test.ts | 51 +++++++++++++++++++++++++++++++
3 files changed, 70 insertions(+), 8 deletions(-)
diff --git a/packages/turf-helpers/index.ts b/packages/turf-helpers/index.ts
index b4fc01ee57..55538fc22f 100644
--- a/packages/turf-helpers/index.ts
+++ b/packages/turf-helpers/index.ts
@@ -598,15 +598,20 @@ export function multiPolygon
(
* // => collection
*/
export function geometryCollection<
+ T extends
+ | Point
+ | LineString
+ | Polygon
+ | MultiPoint
+ | MultiLineString
+ | MultiPolygon,
P extends GeoJsonProperties = GeoJsonProperties,
>(
- geometries: Array<
- Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon
- >,
+ geometries: Array,
properties?: P,
options: { bbox?: BBox; id?: Id } = {}
-): Feature {
- const geom: GeometryCollection = {
+): Feature, P> {
+ const geom: GeometryCollection = {
type: "GeometryCollection",
geometries,
};
diff --git a/packages/turf-line-chunk/index.ts b/packages/turf-line-chunk/index.ts
index 34d8e3da69..41a6770d8d 100644
--- a/packages/turf-line-chunk/index.ts
+++ b/packages/turf-line-chunk/index.ts
@@ -15,7 +15,7 @@ import {
* If the line is shorter than the segment length then the original line is returned.
*
* @function
- * @param {FeatureCollection|Geometry|Feature} geojson the lines to split
+ * @param {FeatureCollection|Geometry|Feature} geojson the LineString or MultiLineStrings to split
* @param {number} segmentLength how long to make each segment
* @param {Object} [options={}] Optional parameters
* @param {Units} [options.units='kilometers'] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}
@@ -34,8 +34,8 @@ function lineChunk(
| Feature
| FeatureCollection
| T
- | GeometryCollection
- | Feature,
+ | GeometryCollection
+ | Feature>,
segmentLength: number,
options: {
units?: Units;
@@ -57,6 +57,12 @@ function lineChunk(
// Flatten each feature to simple LineString
flattenEach(geojson, (feature: Feature) => {
+ if (feature.geometry.type !== "LineString") {
+ throw new Error(
+ "Only LineString and MultiLineString geometry types are supported"
+ );
+ }
+
// reverses coordinates to start the first chunked segment at the end
if (reverse) {
feature.geometry.coordinates = feature.geometry.coordinates.reverse();
diff --git a/packages/turf-line-chunk/test.ts b/packages/turf-line-chunk/test.ts
index 45a61f8de4..8a27f1f6c8 100644
--- a/packages/turf-line-chunk/test.ts
+++ b/packages/turf-line-chunk/test.ts
@@ -106,6 +106,57 @@ test("turf-line-chunk: Prevent input mutation", (t) => {
t.end();
});
+test("turf-line-chunk: Validate behavior on unsupported geometry types", (t) => {
+ // Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
+
+ // each of these `as any` casts are required to bypass the TypeScript rejection and actually test the validation underneath
+ t.throws(() => {
+ lineChunk({ type: "Point", geometry: [0, 0] } as any, 10);
+ }, "Point geometries are rejected");
+
+ t.throws(() => {
+ lineChunk({ type: "MultiPoint", geometry: [[0, 0]] } as any, 10);
+ }, "MultiPoint geometries are rejected");
+
+ t.throws(() => {
+ lineChunk(
+ {
+ type: "Polygon",
+ geometry: [
+ [
+ [0, 0],
+ [1, 0],
+ [1, 1],
+ [0, 0],
+ ],
+ ],
+ } as any,
+ 10
+ );
+ }, "Polygon geometries are rejected");
+
+ t.throws(() => {
+ lineChunk(
+ {
+ type: "MultiPolygon",
+ geometry: [
+ [
+ [
+ [0, 0],
+ [1, 0],
+ [1, 1],
+ [0, 0],
+ ],
+ ],
+ ],
+ } as any,
+ 10
+ );
+ }, "MultiPolygons geometries are rejected");
+
+ t.end();
+});
+
/**
* Colorize FeatureCollection
*