turf.geojsonType
What is turf.geojsonType?
turf.geojsonType is a runtime assertion that throws an Error when a GeoJSON object's type does not match the expected string. It works against any GeoJSON shape — Feature, Geometry, FeatureCollection, or GeometryCollection.
turf.geojsonType(value, type, name) → voidParameters:
value— the GeoJSON objecttype— expected type string (e.g.'Polygon','FeatureCollection')name— the caller's function name, shown in the error message
When would you use turf.geojsonType?
Use turf.geojsonType when writing helpers that need to accept any shape of GeoJSON but only a specific top-level type — a serialiser that only takes FeatureCollections, or a migrator that only converts raw Geometries. It is the most flexible of the four invariant helpers because it does not assume a Feature wrapper.
1function sendToTileService(fc) {
2 turf.geojsonType(fc, 'FeatureCollection', 'sendToTileService');
3 // safe to POST fc as a tile upload
4}FAQs
How do I install Turf.js to use this function?
Install npm install @turf/invariant and import import { geojsonType } from '@turf/invariant', or use turf.geojsonType via the @turf/turf bundle.
Does it check for geometry type or top-level type?
It checks the top-level type field only. For Features that is 'Feature', not the inner geometry type — use turf.featureOf to check the inner geometry.
Does it validate conformance to the GeoJSON spec?
No. It only compares the type string. Use turf.booleanValid for structural validation.
Which invariant helper should I reach for first?
Use geojsonType when any GeoJSON shape is acceptable. Use featureOf / collectionOf when you know you are receiving a Feature or FeatureCollection respectively — they check the inner geometry type too.