Typescript 中的“通用类型‘Feature<T>’需要 1 个类型参数”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36794496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What means "Generic type 'Feature<T>' requires 1 type argument(s)" in Typescript?
提问by dagatsoin
I try to use GeoJson in typescript but the compiler throws error for this two variables: Generic type 'Feature<T>' requires 1 type argument(s)
我尝试在打字稿中使用 GeoJson,但编译器为这两个变量抛出错误: Generic type 'Feature<T>' requires 1 type argument(s)
const pos = <GeoJSON.Feature>{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 1]
}
};
const oldPos = <GeoJSON.Feature>{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [2, 4]
}
};
What is this supposed to mean?
这是什么意思?
采纳答案by Corey Alix
The Feature interface requires a parameter:
Feature 接口需要一个参数:
export interface Feature<T extends GeometryObject> extends GeoJsonObject
{
geometry: T;
properties: any;
id?: string;
}
Try this:
试试这个:
const pos = <GeoJSON.Feature<GeoJSON.GeometryObject>>{
"type": "Feature",
"properties":{},
"geometry": {
"type": "Point",
"coordinates": [0, 1]
}
};
And maybe introduce a helper type and set the type on pos instead of casting will help you ensure you've set the required 'properties' attribute:
也许引入一个辅助类型并在 pos 上设置类型而不是强制转换将帮助您确保您已设置所需的“属性”属性:
type GeoGeom = GeoJSON.Feature<GeoJSON.GeometryObject>;
const pos: GeoGeom = {
type: "Feature",
properties: "foo",
geometry: {
type: "Point",
coordinates: [0, 1]
}
};