A property does not exist in type & ldquo; Interface & quot;

advertisements

I am new to Typescript. I am trying to use interface as an array type. I structure my literal object by order, though I still get this error :

Type '{ id: number; title: string; image: string; words: string; description: string; }[]' is not assignable to type 'IWordList[]'.
Type '{ id: number; title: string; image: string; words: string; description: string; }' is not assignable to type 'IWordList'. Object literal may only specify known properties, and 'id' does not exist in type 'IWordList'.

Notice that the case isn't about 'id' itself, but it is if I added any addtioal property, I get the same error

IWordList.ts

export interface IWordList{
    id : number;
    title : string;
    image : string;
    words : string;
    description : string;
}

wordlistsdata.service.ts

import { Injectable } from '@angular/core';
import { IWordList } from './IWordlist';

@Injectable()
export class WordlistsdataService {

  constructor() { }

  private allWordLists : IWordList[] =
  [
      {
          id : 1,
          title : "Animals",
          image : "/src/app/images/wordlists/animals_background.jpg",
          words : "cat,elephant,cow,duck",
          description : "sdadasd"
      },
      {
          id : 2,
          title : "Jobs",
          image : "/src/app/images/wordlists/jobs_background.jpg",
          words : "engineer,farmer,artist,programmer",
          description : "sdadasd"
      }

    ]

  public getWordList() : IWordList[]{
    return this.allWordLists;
  }

  public getOneWordList(id : number) : string[] {
    return
  }
}


Try with this:

export interface IWordList{
    id ?: number;
    title : string;
    image : string;
    words : string;
    description : string;
}

The ? operator allows you to make things optional.