2017-08-04 3 views
0

私は、インデックス付きストアインスタンスをリスト静的プロパティを持っているしたいのですが、それ悲しいかな、次のエラーで、コンパイルされません。インデックス付きコレクションをtypescriptの静的プロパティとして書き込む方法はありますか?

Type '({ '0750078': Store; } | { '0840021': Store; } | { '0840302': Store; })[]' is not assignable to type '{ string: Store; }[]'. 
    Type '{ '0750078': Store; } | { '0840021': Store; } | { '0840302': Store; }' is not assignable to type '{ string: Store; }'. 
    Type '{ '0750078': Store; }' is not assignable to type '{ string: Store; }'. 
     Object literal may only specify known properties, and ''0750078'' does not exist in type '{ string: Store; }'. 

任意のアイデア?

export class Geolocation { 
    lat:number; 
    long: number; 
    heading: number; 
} 

export class Store { 
    nim: string; 
    name: string; 
    address: string; 
    geolocation: Geolocation; 
    constructor(
     nim: string, 
     name: string, 
     address: string, 
     geolocation: Geolocation) { 

     this.nim = nim; 
     this.name = name; 
     this.address = address; 
     this.geolocation.lat = geolocation.lat; 
     this.geolocation.long = geolocation.long; 
     this.geolocation.heading = geolocation.heading;  
    } 
} 

export class Stores { 
    store: Store; 

    static stores: {string: Store}[] = [ 
     {'0750078': new Store('0750078', 'Kiosque de Paris', 'Place Colette 75001 Paris',    new Geolocation(48.8632, 2.3363, 90))}, 
     {'0840021': new Store('0840021', 'Presse tabac',  'Place de l’église 84140 Montfavet',  new Geolocation(43.9361, 4.8717, 180))}, 
    ]; 
} 

答えて

0

static storesタイプが正しくありません。 Array<{ [x: string]: Store }>

(または{ [x: string]: Store }[]ですが、私はより読みやすいので最初の方が好ましい)

EDIT: あなたがインデックス付きストアをしたい場合、それはする必要があります:あなたが推測されたタイプがうまく動作と同じように、明示的にstoresを入力する必要はありません

export class Stores { 
    store: Store; 

    static stores = { 
    '0750078': new Store('0750078', 'Kiosque de Paris', 'Place Colette 75001 Paris', new Geolocation(48.8632, 2.3363, 90)), 
    '0840021': new Store('0840021', 'Presse tabac', 'Place de l’église 84140 Montfavet', new Geolocation(43.9361, 4.8717, 180)) 
    } 
} 

+0

私はまだ問題に直面しています。Stores.stores ['0840021']は未定義を返しますか?どのように@合同? –

+0

あなたは配列として持っています。 [1]は2番目の要素にアクセスします。それをインデクサにするつもりですか?その場合はオブジェクトでなければなりません – unional

+0

私の最初の質問は、店舗をそのNIM( '0840021'のようなキー)で照合することでした。 –

関連する問題