The primeng MenuItem component does not have a 'command' property and is of type 'never'

I am currently working on implementing a primeng ContextMenu using the Menu Model API. Within the MenuItem object, there is a property named "command" which, to my understanding, should be a function.

As I am receiving the available context menu items from the back-end through an HTTP call, I need to assign this function in the front-end upon data reception.

Although the code compiles and functions correctly, I am encountering an error message in my IDE:

    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;

Upon hovering over the variable with my mouse, I have confirmed that ctxAction is indeed of type MenuItem.

The assignment process looks like this:

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');
}

Could someone please point out what I might be doing wrong here?

Thank you in advance!

Answer №1

The issue was identified when the IDE recognized the implicit type, but the compiler did not catch it (or something to that effect).

Therefore, the problem was resolved by adding a specific type definition in the assignment of the context action list to the corresponding variable. In simpler terms, changing this line:

let ctxActionList = resItem.dtoEntity.ctxActionList;

to this:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting a dynamically loaded Angular 2 module in Chrome and VS Code

Currently, I am utilizing WebPack in conjunction with Angular 2/4 and incorporating modules that are lazy loaded. Due to this setup, the components and modules are not included in the primary .js file; instead, their code is distributed across files genera ...

What is the best way to set up JSData with cookie-based sessions and CSRF headers?

In order to properly configure my JSData settings, I must include instructions for passing information related to cookie-based session authentication and CSRF headers. ...

Hiding a specific tag with vanilla JavaScript based on its content

I am facing a challenge with my code that is supposed to hide div elements containing a specific word along with additional text. I have tried multiple solutions but none seem to work effectively. Any assistance on how to hide divs properly will be greatl ...

How do I fix the build error that says "Operator '+' cannot be used with types 'number[]'?

The function below is designed to generate unique uuidv4 strings. function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)) ...

Customizing the Style of Mat-Form-Field

I have designed a search bar using mat-form-field and attempted to personalize the appearance. Currently, there is a gray border-like region surrounding the input field and icons. Moreover, clicking on the input field results in a visible border: <form ...

Looking to display the ng-option in two lines within Angular 6?

How can I display data in a select dropdown with two lines for each option? I am currently using ng-select. <ng-select [(ngModel)]="selectedData" placeholder="Select Data"> <div *ngFor="let data of Data"> <ng-option [value]="da ...

Merging Two Individual Applications into a Single Application Using Angular 8

Recently started learning Angular as a beginner. I am currently working on developing an application and opted to utilize the Angular Multikart ecommerce template. This template comes with separate front-end and admin-end sections, which I can currently ...

The traditional function does not have access to a reference to this

For my web development project with Angular 9, I needed to add a typeahead feature using ng bootstrap typeahead. The code provided below worked perfectly: search = (text$: Observable<string>) => text$.pipe( debounceTime(150), disti ...

Enhance your Angular2+ writing skills by delving into the intricacies of subscriptions with `

Seeking assistance to enhance the code structure. I am struggling with understanding switchMap, mergeMap and how to avoid nested subscriptions. this.service .service1('something') .pipe(takeUntil(this.unsubscribe)) .subscribe( (s1result ...

Discovering the specific p-checkbox in Angular where an event takes place

Is there a way to assign an ID to my checkbox element and retrieve it in my .ts file when the checkbox is selected? I want to pass this ID along with the true or false value that indicates if the checkbox is checked. Here's an example code snippet: &l ...

What is the best way to retrieve the final value stored?

This is how I am using my Selector:- private loadTree() { this.loading = true; this.store.select(transitionListSelector).pipe().subscribe(data => { console.log(data); data.map(item => { console.log(item); this.tr ...

Encountering problems with createMediaElementSource in TypeScript/React when using the Web Audio API

Currently, I am following a Web Audio API tutorial from MDN, but with a twist - I am using TypeScript and React instead of vanilla JavaScript. In my React project created with create-react-app, I am utilizing the useRef hook to reference the audio element ...

Eliminate perfect-scrollbar functionality on mobile devices

I have applied the perfect-scrollbar class with 'height: auto' on mobile screens, expecting it to work properly. It's functioning well in responsive mode on the web. However, it does not scroll on mobile devices. @media (max-width: 992px) { ...

Guide on utilizing jasmine's spy functionalityAlways remember to use the

I am a beginner when it comes to Angular and Jasmine, and I am encountering difficulties while attempting to mock: public uploadFile(confirm) { this.uploadModalRef.close(); if (this.filePath.length) { let ngbModalOptions: NgbModalOptions = { ...

Guide on testing a function with a dependency in Angular through unit testing

Attempting to dive into unit testing, I have grasped some of the basics. However, my current challenge lies in testing a method within my code. This particular method involves calling a function from oidc-client.js that handles user sign-ins. My spec fi ...

Is it possible to bind parameters in the select clause using TypeORM?

I'm currently working on implementing a search feature using the pg_trgm module in my PostgreSQL project built with TypeScript and TypeOrm. My SQL query that works for me looks like this: SELECT t, similarity(t, 'word') AS sml FROM test_t ...

A more efficient method for querying documents based on ids that are not in a given list and then sorting them by a specific publish date

After implementing the code provided below, I noticed that the performance tests indicate each request takes a second or longer to complete. My goal is to enhance this speed by at least 10 times. The bottleneck seems to be caused by the NOT operator resu ...

Upon completing the upgrade of an Angular project from version 9.1 to 16, I encountered an error stating "catch (next.handle(...).catch is not a function)."

I successfully built a new Angular 16 project from scratch, copied all the code files, and updated the npm packages. However, I encountered an error during smoke tests related to the httpClient catch method. Below is the code for the interceptor: import { ...

Conceal multiple parameters within routing for added security

I have setup my Angular component with a button that triggers an event. Inside this event, I currently have: this.router.navigate('page2') While I am aware that query parameters can be passed inside the URL, I am faced with the challenge of pas ...

Angular 8: Issue with setErrors not reflecting on the template when marking a control as invalid

In my component, I am using the following code: this.frmGroup.controls['dorms'].setErrors({'incorrect': true}); For debugging purposes, I have added the following to my template: {{this.frmGroup.controls['dorms'].invali ...