Skip to content

break(helpers) tighten geometry() coordinates typing - #3166

Open
modusensus wants to merge 1 commit into
Turfjs:masterfrom
modusensus:break-helpers-geometry-coordinates
Open

modusensus wants to merge 1 commit into
Turfjs:masterfrom
modusensus:break-helpers-geometry-coordinates

Conversation

@modusensus

Copy link
Copy Markdown

Summary

Tightens the coordinates parameter of geometry() so TypeScript enforces the correct coordinate shape for each geometry type at compile time:

  • "Point"Position
  • "LineString" / "MultiPoint"Position[]
  • "Polygon" / "MultiLineString"Position[][]
  • "MultiPolygon"Position[][][]

Resolves #2973

Breaking change

⚠️ Compile-time only — no runtime behavior change. Call sites passing arrays with the wrong nesting depth (or other loose arrays) will now fail type checking. This follows the type-strictness direction of #3160#3163.

Design notes (open to feedback)

  • CoordinatesFor<T> is deliberately not exported. Exporting it would add a conditional type to the public API that the docs generator cannot resolve (it renders as Type: any in the README). If you'd prefer it exported for downstream generic code, that's a one-line change — happy to flip it.
  • GeometryType is exported because it resolves cleanly in the generated docs, and it gives consumers a reusable name for the six geometry kinds geometry() accepts. The name is bikesheddable (GeometryTypeName, GeometryTypeString, …).
  • The documentation generator renders parameter types from the TypeScript signatures rather than the @param annotations, which is why the README shows coordinates as CoordinatesFor<T> instead of a Position|Position[]|… union.

Changes

  • New exported GeometryType union and internal CoordinatesFor<T> conditional type in @turf/helpers
  • geometry()'s runtime logic is unchanged; a single re-broadening alias bridges the deferred generic to the per-type constructors in the switch
  • Positive and negative type assertions added in packages/turf-helpers/types.ts (the package's existing test:types convention)
  • README regenerated via pnpm run docs
  • Added my details to @turf/helpers' contributors field per the PR checklist

Checklist

Full pnpm -r run test passing locally across all 122 packages (the root lint suite could not run on this Windows machine due to a monorepolint Windows path bug — CI will cover it).

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.

Increase type strictness of @turf/helper's geometry() coordinates

1 participant