turf.featureOf
What is turf.featureOf?
turf.featureOf is a runtime assertion that throws an Error if a Feature's geometry type does not match a target string. It is used by Turf functions that expect a specific geometry and is exported for your own helpers.
turf.featureOf(feature, type, name) → voidParameters:
feature— the Feature to validatetype— expected geometry type (e.g.'Polygon')name— the caller's function name, included in the error
When would you use turf.featureOf?
Use turf.featureOf inside your own API surface when a function requires one geometry type — an area calculator that wraps turf.area but only accepts Polygons, or a routing helper that only accepts LineStrings. The thrown error mirrors the style of Turf core.
1function polygonPerimeter(feature) {
2 turf.featureOf(feature, 'Polygon', 'polygonPerimeter');
3 return turf.length(turf.polygonToLine(feature));
4}FAQs
How do I install Turf.js to use this function?
Install npm install @turf/invariant and import import { featureOf } from '@turf/invariant', or use turf.featureOf from the umbrella bundle.
Does featureOf accept bare geometries?
No — it expects a Feature wrapper. Use turf.geojsonType or wrap the geometry with turf.feature before calling.
Can it accept multiple types?
No, type is a single string. Validate against multiple types with two calls inside a try/catch, or check the type manually.
When should I use featureOf vs collectionOf?
Use featureOf for a single Feature input. Use turf.collectionOf when every Feature in a FeatureCollection must match a type.