Angular 2: model ts class composed of another model ts

I have a Hero and an Address model class, one of the properties of a Hero is its Address which is composed of: street, city…. What’s the correct way to specify this in the concrete model source class?

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;
}

mock-heroes.ts:

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 when I run “npm start”:

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’.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Its pretty simple:

...,'address' : {street:'a', street2:'b', city:'c', state:'d'}, ...