2016-04-22 22 views
6

Io cerco di usare GeoJSON a macchina ma il compilatore genera un errore per questo due variabili: Generic type 'Feature<T>' requires 1 type argument(s)Cosa significa "Tipo generico" Caratteristica <T> "richiede 1 tipo di argomento" in Typescript?

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

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

Cos'è questo vorrebbe dire?

+0

Prova > –

risposta

3

L'interfaccia funzione richiede un parametro:

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

Prova questa:

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

E forse introdurre un tipo di aiuto e impostare il tipo su pos Invece di lanciare vi aiuterà a garantire che hai impostare l'attributo richiesto 'proprietà':

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

dispiace per la risposta tardiva, ma sì sì funziona. Potresti aggiungere "proprietà": {} nel blocco di codice dopo "Prova questo". Non è valido in questo modo, manca la proprietà "proprietà". Grazie ! – dagatsoin