diff --git a/packages/turf-invariant/README.md b/packages/turf-invariant/README.md index 381e2d58e..a8255db7b 100644 --- a/packages/turf-invariant/README.md +++ b/packages/turf-invariant/README.md @@ -132,8 +132,6 @@ Get GeoJSON object's type, Geometry type is prioritize. ### Parameters * `geojson` **[GeoJSON][7]** GeoJSON object -* `_name` **[string][8]?** -* `name` **[string][8]** name of the variable to display in error message (unused) (optional, default `"geojson"`) ### Examples diff --git a/packages/turf-invariant/index.ts b/packages/turf-invariant/index.ts index a672b17bd..2480a72e5 100644 --- a/packages/turf-invariant/index.ts +++ b/packages/turf-invariant/index.ts @@ -250,7 +250,6 @@ function getGeom(geojson: Feature | G): G { * Get GeoJSON object's type, Geometry type is prioritize. * * @param {GeoJSON} geojson GeoJSON object - * @param {string} [name="geojson"] name of the variable to display in error message (unused) * @returns {string} GeoJSON type * @example * var point = { @@ -264,20 +263,19 @@ function getGeom(geojson: Feature | G): G { * var geom = turf.getType(point) * //="Point" */ -function getType( - geojson: Feature | FeatureCollection | Geometry, - _name?: string -): string { - if (geojson.type === "FeatureCollection") { - return "FeatureCollection"; - } - if (geojson.type === "GeometryCollection") { - return "GeometryCollection"; - } +function getType< + T extends Geometry | Feature | FeatureCollection, +>( + geojson: T +): T extends Feature + ? G extends Geometry + ? G["type"] + : "Feature" + : T["type"] { if (geojson.type === "Feature" && geojson.geometry !== null) { - return geojson.geometry.type; + return geojson.geometry.type as any; } - return geojson.type; + return geojson.type as any; } export { diff --git a/packages/turf-invariant/test.ts b/packages/turf-invariant/test.ts index 82e6af889..ab3cc620b 100644 --- a/packages/turf-invariant/test.ts +++ b/packages/turf-invariant/test.ts @@ -5,8 +5,10 @@ import { polygon, featureCollection, geometryCollection, + feature, } from "@turf/helpers"; import * as invariant from "./index.js"; +import { LineString, Point } from "geojson"; test("invariant -- containsNumber", (t) => { t.equals(invariant.containsNumber([1, 1]), true); @@ -367,13 +369,15 @@ test("invariant -- getType", (t) => { [0, 1], [1, 1], ]); - const collection = featureCollection([pt, line]); + const collection = featureCollection([pt, line]); const geomCollection = geometryCollection([pt.geometry, line.geometry]); + const nullFeature = feature(null); t.deepEqual(invariant.getType(pt), "Point"); t.deepEqual(invariant.getType(line.geometry), "LineString"); t.deepEqual(invariant.getType(geomCollection), "GeometryCollection"); t.deepEqual(invariant.getType(collection), "FeatureCollection"); + t.deepEqual(invariant.getType(nullFeature), "Feature"); // t.throws(() => invariant.getType(null), /geojson is required/, 'geojson is required'); t.end(); });