TS2339: The specified property is not present on the Angular type

Hey everyone, I've recently embarked on my Angular learning journey.

Unfortunately, I'm currently facing this Error:

32  this.warning.error.push(entry.name+': '+entry.error);
                 ~~~~~
src/app/dashboard/dashboard.component.ts:33:15 - error TS2339: Property 'id' does not exist on type 'Warning[]'.

33  this.warning.id.push(entry.id);
                 ~~
src/app/dashboard/dashboard.component.ts:37:13 - error TS2339: Property 'error' does not exist on type 'Error[]'.

37  this.error.error.push(entry.name+': '+entry.error);
               ~~~~~
src/app/dashboard/dashboard.component.ts:38:13 - error TS2339: Property 'id' does not exist on type 'Error[]'.

38  this.error.id.push(entry.id);
               ~~

The issue lies in the fact that I have defined Interfaces for both of them and have imported them as well.

export interface Error {
        id: number;
        error: string;
        }

export interface Warning {
        id: number;
        error: string;
        }

You can see how they are implemented in my component below.

import { Error, Warning } from '../dashboard';
...
export class DashboardComponent implements OnInit {
 error: Error[];
 warning: Warning[];
...

evaluate(): void{
        for (let entry of this.status){
        if (entry.status === 0){
                this.ok = this.ok + 1;
                }
        if (entry.status === 1 && entry.value < 8){
        this.warnings = this.warnings + 1;
        this.warning.error.push(entry.name+': '+entry.error);
        this.warning.id.push(entry.wt_id);
                }
        if (entry.status === 1 && entry.value >= 8){
        this.critical = this.critical + 1;
        this.error.error.push(entry.wt_name+': '+entry.error);
        this.error.id.push(entry.wt_id);
                }
        }
}

I have tried various methods mentioned in previous posts but to no avail.

If any of you have a solution or spot a mistake, I would greatly appreciate your input. Maybe it's just something small that slipped my notice.

Cheers!

Answer №1

Make sure you are pushing values to the properties within the member variables error and warning, not directly to the variables themselves.

  1. To resolve this, consider changing the type definitions to arrays.

Interfaces

export interface Error {
  id: number[];
  error: string[];
}

export interface Warning {
  id: number[];
  error: string[];
}

Component

export class DashboardComponent implements OnInit {
  error: Error;
  warning: Warning;
 
  ...
}
  1. Alternatively, initialize the arrays and push values to the member variables as suggested by @GunnerB.

Interfaces

export interface Error {
  id: number;
  error: string;
}

export interface Warning {
  id: number;
  error: string;
}

Component

export class DashboardComponent implements OnInit {
  error: Error[] = [];              // <-- ensure arrays are initialized
  warning: Warning[] = [];
  ...

  evaluate(): void {
    for (let entry of this.status) {
      if (entry.status === 0) {
        this.ok = this.ok + 1;
      }
      if (entry.status === 1 && entry.value < 8) {
        this.warnings = this.warnings + 1;
        this.warning.push({id: entry.wt_id, error: entry.name + ': ' + entry.error });
      }
      if (entry.status === 1 && entry.value >= 8) {
        this.critical = this.critical + 1;
        this.error.push({id: entry.wt_id, error: entry.name + ': ' + entry.error });
      }
    }
  }
}

Answer №2

Could you please update to: `this.warning.error = entry.wt_name + ': ' + entry.error;

error within the Warning interface should be a string and not an array.

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

The repository injected into NestJs using TypeORM suddenly becomes null

In my code, I am fetching Requisition data from an external system using the following approach: init() { const requisitionData = this.loginMb().pipe( map(response => response.data.token), switchMap(loginData => this.getRequisitions(loginD ...

Make sure that the correct type of 'this' is recognized

Consider this code snippet: const test = { a: 1, b() { const test = this.a; // works const test2 = this.c // fails } } In Typescript, the type of this is automatically detected. However, when I implement the same code in my project, there ar ...

Reference loss occurs in Angular Ionic when using @ViewChild

This situation is straightforward I am dealing with a reference in this format @ViewChild('myElement') myElementVar : SomeClass; The element I am referencing appears like this <element #myElement *ngIf="someBoolean"></element> As ...

typescript error is not defined

While browsing online, I came across a post discussing how to transfer data from an MVC model to a .ts file. The suggestion was to include the following code: <script type="text/javascript"> var testUrl = @Html.Raw(Json.Encode(Model.testUrl) ...

What steps should I take in order to correctly implement the onChange event and retrieve the event.target.value in my

Incorporating useForm, yupResolver, and Yup for validation caused issues with the previously functioning handleChange function. The value variable doesn't log anything when console.logged, indicating a disconnect with the input field content. Addition ...

When a URL is entered, information is not retrieved from the database

I have a simple app setup where the data (iPhones from the database) is fetched in the AppComponent itself. ngOnInit(): void { this.iphoneservice.fetchIphones(); } The fetchIphones method is located in my iPhoneService file and consists of 3 functio ...

Get access to documents via angular2 - ionic 2

I am currently working on an Ionic 2 app and I am facing the challenge of downloading audios from SoundCloud. I have already obtained the necessary URL for making the request: let url = `https://api.soundcloud.com/tracks/${audio.id}/download?client_id=${t ...

magnify within a div with a set width

Looking for a way to make a div both zoomable and scrollable within a fixed-width container using transform: scale(aScaleRatio) for zooming in/out. Check out this demo for achieving the zoom effect. However, the inner div seems to have trouble aligning to ...

Anticipated that 'styles' would comprise an array of strings in Angular2

Whenever I encounter this issue Uncaught Error: Expected 'styles' to be an array of strings. Below is my code example styleUrls:[ "../../styles/login.component.styles.scss" ], Interestingly, when I declare them in a similar manner but ...

An ambient module will not be successfully resolved through a relative import operation

As per the typescript documentation (https://www.typescriptlang.org/docs/handbook/module-resolution.html): A relative import is resolved in relation to the importing file and does not resolve to an ambient module declaration. However, it also states: ...

Encountered an error while using npm install --legacy-peer-deps: Debug Failure. SyntaxKind: Unknown went unhandled

When attempting to execute the command npm install --legacy-peer-deps, I encountered an error. The specific error message is as follows: Error: Error on worker #1: Error: Debug Failure. Unhandled SyntaxKind: Unknown. at pipelineEmitWithHintWorker (port ...

Is it possible to use Angular *ngIf on a component while also binding ngStyle to a method

Currently, I am working with Angular 5 and grappling with an issue that may be related to either the Angular version or how I structured my components. The primary focus of my task involves creating a component resembling a table, comprising two key segme ...

Populate Textboxes with Database Data when Textbox Value is Updated

When the name textbox is changed to "Mary", the corresponding values "Spoon" and "Plate" from the database table should be filled in the store A and store B textboxes, respectively. Id Name Store A Store B 1 Julie Knife Pot 2 Mary Spoon Plate Com ...

Is it possible to pass generics into a Promise, such as Promise<T>?

Here's the code snippet for a function I am working with: class RestService { public async get<T>(func: string): Promise<T> { var toRet = {}; await fetch(EndPoint + func) .then(response => response.json() as ...

Customizable TypeScript interface with built-in default key value types that can be easily extended

One common pattern that arises frequently in my projects involves fetching values and updating the UI based on the 'requestStatus' and other associated values. type RequestStatus = | 'pending' | 'requesting' | 'succ ...

What could be preventing a button click from happening when using TypeScript?

Can you please explain why the button click event is not working in TypeScript? Below is the code snippet I am referring to: https://jsfiddle.net/z4vo5u5d/238/ class Nav { private config:object; constructor(){ this.config = { ...

Efficient configuration for pnpm monorepo with TypeScript, vite, and rollup

Struggling to set up a monorepo using pnpm workspaces with typescript, vite for frontends, and rollup for backend microservices. Here's the current project structure: package.json <== all dependencies reside here tsconfig ...

Leverage the power of ssh2-promise in NodeJS to run Linux commands on a remote server

When attempting to run the command yum install <package_name> on a remote Linux server using the ssh2-promise package, I encountered an issue where I couldn't retrieve the response from the command for further processing and validation. I' ...

You need to have an Angular project in order to use the serve command, however, the project definition could

I've been given a task that involves running the ng serve command in a specific folder. The folder structure I'm working with is as follows: ProjectFolder - > ClientApp - >Src -> dist ...

The Ins and Outs of Selecting the Correct Module to Attach a Controller in NestJS CLI

My experience with NestJS has been great so far, especially the Module system and how easy it is to parse requests. However, I have a question about the NestJS CLI. Let's say I have multiple modules. When I create a controller using the command "nes ...