1

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[] = [...];
2
  • Does it have to be an interface? Cannot you just type alias an array? type todo = { ... }[];? Commented Aug 4, 2020 at 9:17
  • Actually, early I described it as Object[], but my friend told me that the better way is to describe it with interface Commented Aug 4, 2020 at 9:26

1 Answer 1

1

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[] = [...];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.