2016-04-17 4 views
0

私はHeroとAddressモデルクラスを持っています.Heroのプロパティの1つは、street、city ...で構成されている住所です。具体的なモデルのソースクラスでこれを指定するには?角2:別のモデルで構成されたモデルtsクラス

hero.ts:

import {Address} from './address'; 
export class Hero { 
    id: number; 
    name: string; 
    powers: string []; 

    address: Address; 
} 

address.ts:

export class Address { 
    street: string; 
    street2: string; 
    city: string; 
    state: string; 
} 

モックheroes.ts:私は "NPM開始" を実行し

import {Hero} from './hero'; 
export var HEROES: Hero[] = [ 
    {"id": 11, "name": "Mr. Nice", "powers" : ['nice', 'helpful'], "address" : ("a", "b", "c", "d")}, 
    {"id": 12, "name": "Narco", "powers" : ['nice', 'helpful'], "address" : ("a", "b", "c", "d")} 
]; 

ERROR:

app/mock-heroes.ts(3,12): error TS2322: Type '{ "id": number; "name": string; "powers": string[]; "address": string; }[]' is not assignable to type 'Hero[]'. 
    Type '{ "id": number; "name": string; "powers": string[]; "address": string; }' is not assignable to type 'Hero'. 
    Types of property 'address' are incompatible. 
     Type 'string' is not assignable to type 'Address'. 

答えて

3

かなり簡単:

...,'address' : {street:'a', street2:'b', city:'c', state:'d'}, ... 
関連する問題