2

I'm new to Angular and I believe I've set up my mock data the wrong way and that results in the following error:

Argument of type '{ "name": string; "refCode": string; "contentId": string; "enabled": boolean;  }[]; } | { ...; }' is not assignable to parameter of type 'Item'.

Updated with additional error messages:

Types of property ... are incompatible.
  Type '{ "name": string; "refCode": string; "contentId": string }[]' is not assignable to type 'Item[]'.
    Property ... is missing in type '{ "name": string; "refCode": string; "contentId": string;}' but required in type 'Item'.

Here's my json file contents:

{
  "data": {
    "items": [
      {
        "obj1": "one",
        "obj2": "two",
        "obj3": three
      },
      {
        "obj1": "four",
        "obj2": "five",
        "obj3": siix
        "subCategories": [
          {
          }
        ]
      },
      {
        "name": "Menu Option 1",
        "refCode": "opt1",
        "contentId": "0",
        "subCategories": [
          {}
   
        ]
      },
      {
        "name": "Menu Option 2",
        "refCode": "opt2",
        "contentId": "0"
      },
      {
        "name": "Menu Option 3",
        "refCode": "opt3"
      }
    ]
  }
}

In my service class I import it like this:

import dataItems from './shared/my-items.json';

And declare it like this:

public menuItems: any;

Then I iterate over the items here in my component class:

const optionData = 
this.itemService.getMenuItems().data.items;
const resource = {};
if (itemData && itemData.length > 0) {
  this.items = [];
  itemData.forEach(element => {
    this.itemService.applyItemName(
      element
    );
    this.items.push(element 
  });
  this.itemService.categories = this.itemss;
}

Lastly is the applyItemName() method which adds an item and its children for a given Item:

public applyItemName(item: Item) {
if (item.subCategories && item.subCategories.length > 0) {
  item.subCategories.forEach(subCategory => {
    subCategory.parent = category;
    this.applyItemName(subCategory);
  });
}

}

The model for a Item object is here:

export class Category {
  public subCategories: Items[];
  public contentId: string;


  constructor(subCategories: Item[], enabled: boolean, name: string) 
  {
      this.subCategories = subCategories;
      this.enabled = enabled;
      this.name = name;
    }
  }

As I said, I'm new to this and I'm not sure what I'm missing. Any tips are greatly appreciated.

2
  • 2
    The error should include a message about why it's not assignable usually along the lines of "missing parameter Foo" or some such. Commented Mar 11, 2020 at 13:45
  • You're right, thank you. I'll add the missing portion of the error message to the post. Commented Mar 11, 2020 at 13:47

1 Answer 1

2

I think the issue there is childCategories should be optional, since in some childs in your object it is not present. Adding ? at declaration will make it optional:

export class Category {
  public childCategories?: Category[];

Or you could always initialise childCategories as an empty array

  public childCategories: Category[] = [];
Sign up to request clarification or add additional context in comments.

1 Comment

That was it - thank you. The previous commenter alerted me to additional error messages and I've updated the original post with those. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.