Vagner Wentz
Vagner Wentz

Reputation: 566

How to build a interface that inside have array object

Objective

Build an interface that inside will have telephone_array. I know that my telephone_array is an array that has many objects inside.

enter image description here

Interfaces that I have

interface Client {
  id: string;
  name: string;
  email: string;
  telephone: string;
  created_at: string;
}


interface ClientsResponse {
  clients: Client[];
}

I would like to know how can I put the telephone_array inside the interface.

Upvotes: 0

Views: 43

Answers (3)

johannchopin
johannchopin

Reputation: 14873

Just use another interface as an array:

interface Telephone {
  id: string;
  telephone_number: string;
}

interface Client {
  id: string;
  name: string;
  email: string;
  telephone: string;
  created_at: string;
  telephone_array: Telephone[];
}

Upvotes: 0

lanxion
lanxion

Reputation: 1430

You can add array of objects like so:

interface Client {
  id: string;
  name: string;
  email: string;
  telephone: string;
  created_at: string;
  telephone_array: Array<{
     id: string;
     telephone_number: string;
  }>
}

or

telephone_array: {
     id: string;
     telephone_number: string;
  }[]

Upvotes: 2

invisal
invisal

Reputation: 11181

Something like this

interface Client {
  id: string;
  name: string;
  email: string;
  telephone: string;
  created_at: string;
  telephone_array: {
     id: string;
     telephone_number: string;
  }[];
}

Upvotes: 1

Related Questions