user9779830
user9779830

Reputation: 29

How to define in Interface object array and work with it?

I have an object array by this example:

 tasks: todo =  [
      {
        title: string,
        status: number,
        description: string,
        date: Date,
        priority: number
      }
 ]

So I create an Interface for this:

interface todo {
  [index: number]:{
    title: string;
    status: number;
    description: string;
    date: Date;
    priority: number;
  }
}

and when I give to a variable, which has object array this interface, I've got errors: Property 'filter' does not exist on type 'todo' and Property 'sort' does not exist on type 'todo'. How to prevent these errors?

EDIT : Found the solution:

export interface todo extends Array <{
  title: string;
  status: number;
  description: string;
  date: Date;
  priority: number;
}> {}

Also the answer here is good too:

interface Todo {
  title: string;
  status: number;
  description: string;
  date: Date;
  priority: number;
}

// tasks is an array of Todo
tasks: Todo[] = [...];

Upvotes: 1

Views: 38

Answers (1)

pzaenger
pzaenger

Reputation: 11992

Your interface should describe a single task/todo:

interface Todo {
  title: string;
  status: number;
  description: string;
  date: Date;
  priority: number;
}

// tasks is an array of Todo
tasks: Todo[] = [...];

Upvotes: 1

Related Questions