Согласно типам функция feature()
возвращает либо Feature
, либо FeatureCollection
.Только FeatureCollection
будет иметь атрибут .features
, который вы ищете.
Проверка кода пакета TopoJSON (строки 4 - 8), мы видим, что a FeatureCollection
возвращается, только если topology
имеет GeometryCollection
в качестве type
.
export default function(topology, o) {
return o.type === "GeometryCollection"
? {type: "FeatureCollection", features: o.geometries.map(function(o) { return feature(topology, o); })}
: feature(topology, o);
}
Вы загружаете topology
асинхронно, поэтому для компилятора невозможно узнать, является ли.type
равно GeometryCollection
или нет.
Для решения этой проблемы вам необходимо установить типы GeoJSON (npm i @types/geojson
).
Затем можно установитьтип временной переменной
...
d3.json("../../assets/plz_map_ger.json")
.then(function(top:any) {
// This row sets the temporary variable
let mapFeatures: FeatureCollection = t.feature(top, top.objects.plz5stellig)
g.selectAll('path')
// We use the temporary variable here
.data(mapFeatures.features)
.enter()
.append('path')
.attr('d', path)
.attr("class","kreisgrenzen")
.on("click", function() {
d3.select(this).attr("class","selected-kreis");
});
});
Или вы можете явным образом привести коллекцию в коллекцию объектов (благодаря @altocumulus)
...
d3.json("../../assets/plz_map_ger.json")
.then(function(top:any) {
g.selectAll('path')
// explicit cast
.data((t.feature(top, top.objects.plz5stellig) as GeometryCollection).features)
.enter()
.append('path')
.attr('d', path)
.attr("class","kreisgrenzen")
.on("click", function() {
d3.select(this).attr("class","selected-kreis");
});
});