cnak2
cnak2

Reputation: 1841

Trying to add array to TS interface

I have an angular 2 project and I'm fairly new. I'm trying to create an interface that includes object arrays for things like phone numbers and emails. So for example, I want to be able to have a collections of emails and type them (personal, business, etc.). In my interface I'm doing the following for this 'emails' array.

[emails:{email_address:string, email_type:string, }]:any;

The error message I get is:

[ts] An index signature parameter type must be 'string' or 'number'.

(parameter) emails: {
    email_address: string;
    email_type: string;
}

I thought this message meant I needed to put quotes around 'emails' so that it was a string, but that's not right either.

What am I doing wrong here?

Thanks all!

Here is my interface:

export interface IContact {
    //CONTACT INFO
      owner_id:string;
        share_id?: string;
        accepted?:boolean;
        status?:number;
        first_name?:string;
        last_name?:string;
        company?:string;
        title?:string;
        initial?:string;
        birthday?:Date;
        highschool?:string;
        college?:string;
        facebook?:string;
        linkedin?:string;
        linkedin_bus?:string;
        twitter?: string;
        google?:string;
        pinterest?:string;
        user_image?:string;
        emails: Email[];
        phones: Phone[];
        addresses: Address[];

}

    interface Email {
        email_type: string;
        email_address: string;
    }

    interface Phone {
        phone_type: string;
        phone_number: string;
    }

    interface Address {
        address_type: string;
        address: string;
        address2: string;
        city: string;
        state: string;
        zip: string;
    }

By the way, I call it in the controller like this:

  contacts: IContact;

Upvotes: 0

Views: 165

Answers (3)

Anuradha Gunasekara
Anuradha Gunasekara

Reputation: 6983

Hope this helps.

export interface YourInterfaceName {
  emails: Email[];
  phoneNumbers: Phone[];
}

interface Email {
  email_address: string;
  email_type: string;
}

interface Phone {
  phoneNumber: string;
  type: string;
}

And in your component.ts. Yo can do something like this.

const userData: YourInterfaceNam;

userData.emails.push({
    email_address: '[email protected]',
    email_type: 'work'
  },
  {
    email_address: '[email protected]',
    email_type: 'home'
  }
);


userData.phoneNumbers.push({
    phoneNumber: '56316565',
    type: 'home'
  },
  {
    phoneNumber: '456132561',
    type: 'work'
  }
);

console.log(userData.emails[0].email_address); // [email protected]

Upvotes: 2

John
John

Reputation: 11399

What did you name your interface? You can use the name of the interface as a type.

let emails: MyEmailInterface;

Or the same as you have, but you are missing the array:

let emails: Array<{
    email_address: string;
    email_type: string;
}>;

Upvotes: 0

Lia
Lia

Reputation: 11982

try :

private test : {emails:{email_address:string, email_type:string }}[]

Upvotes: 2

Related Questions