Daniel Methner
Daniel Methner

Reputation: 607

primeng MenuItem: Property 'command' does not exist on type 'never'

I want to implement a primeng ContextMenu which uses the Menu Model API. The MenuItem object has a property called "command" which is as per my understanding is a function.

Since my back-end is sending the available context menu items via a HTTP call, I need to assign this function in the front-end when receiving the data.

The code compiles and works as expected, however my IDE shows an error:

    ERROR in src/app/protected/workbench/sidebar/sidebar.component.ts:61:21 - error TS2339: Property 'command' does not exist on type 'never'.

61           ctxAction.command = this.execCtxAction;

ctxAction is of type MenuItem, as confirmed when I hover over the variable with my mouse.

The assignment is done as follows:

public getFavourites() {
  let url = "http://localhost:8081/api/fav/all";
  this.http.get<Fav[]>(url).subscribe(
    res => {
      for (const resItem of res) {
        let ctxActionList = resItem.dtoEntity.ctxActionList;
        // the variable ctxAction is of type MenuItem[]
        for (let ctxAction of ctxActionList) {
          ctxAction.command = this.execCtxAction;
        }
      }
      this.fav = res;
    },
    err => { alert("there is an error") }
  );
}

execCtxAction(): void {
  console.log('execute context action');
}

What am I doing wrong here?

Many thanks in advance!

Upvotes: 0

Views: 642

Answers (1)

Daniel Methner
Daniel Methner

Reputation: 607

Found the issue. While the IDE recognised the implicit type, the compiler did notb (or something along those lines).

Hence the issue was fixed by including a specific type definition in the assignment of the context action list to the corresponding variable. Or in other words, changing this line:

let ctxActionList = resItem.dtoEntity.ctxActionList;

into this:

let ctxActionList: MenuItem[] = resItem.dtoEntity.ctxActionList;

Upvotes: 0

Related Questions