2016-04-22 3 views
6

私はtypescriptです中にGeoJSONを使用しようとするが、コンパイラは、この二つの変数のためにエラーがスローされます:Generic type 'Feature<T>' requires 1 type argument(s)typescriptに "Generic type 'Feature <T>'が1つの型引数を必要とするとはどういう意味ですか?

const pos = <GeoJSON.Feature>{ 
    "type": "Feature", 
    "geometry": { 
     "type": "Point", 
     "coordinates": [0, 1] 
    } 
    }; 

    const oldPos = <GeoJSON.Feature>{ 
    "type": "Feature", 
    "geometry": { 
     "type": "Point", 
     "coordinates": [2, 4] 
    } 
    }; 

これが意味する何を想定していますか?

+0

試してみてください> –

答えて

3

機能のインターフェースは、パラメータが必要です。

const pos = <GeoJSON.Feature<GeoJSON.GeometryObject>>{ 
    "type": "Feature", 
    "properties":{}, 
    "geometry": { 
     "type": "Point", 
     "coordinates": [0, 1] 
    } 
    }; 

そしておそらくヘルパーの種類を紹介し、あなたがきたことを確認するのに役立ちます代わりに、鋳造のPOSの種類を設定します。

export interface Feature<T extends GeometryObject> extends GeoJsonObject 
{ 
    geometry: T; 
    properties: any; 
    id?: string; 
} 

を、これは試してみてください必要な 'プロパティ'属性を設定します。

type GeoGeom = GeoJSON.Feature<GeoJSON.GeometryObject>; 
const pos: GeoGeom = { 
    type: "Feature", 
    properties: "foo", 
    geometry: { 
     type: "Point", 
     coordinates: [0, 1] 
    } 
}; 
+0

遅い返事のために、しかし確かにそれはうまくいきます。 "試してください"の後にコードブロックに "プロパティ":{}を追加してください。これは、この方法では有効ではなく、「プロパティ」プロパティがありません。あなたよ! – dagatsoin

関連する問題