When initializing an array of interfaces in TypeScript, properties may not be recognized

I've designed a new interface that I plan to incorporate into my custom Angular component.

Here is the Interface:

export interface Gadget {
    constructor(
        gadgetID: string,
        gadgetName: string,
        gadgetModel: string,
        buildYear: string,
        gadgetOS: string
    );
}

Custom component implementation:

import { Component, OnInit} from "@angular/core";
import { Gadget } from "src/shared/interfaces/gadget";

@Component({
    selector: 'gadget-dashboard',
    templateUrl: './gadget-dashboard.component.html',
    styleUrls: ['./gadget-dashboard.component.less']
})
export class GadgetDashBoardComponent implements OnInit{
    constructor() {}

    gadgets: Gadget[] = [
        {
            gadgetID: "12345",
            gadgetName: 'name',
            gadgetModel: 'model',
            buildYear: '2015',
            gadgetOS: 'Ubuntu 22.04',
        },
    ];

    ngOnInit(): void {     }

However, I am facing an issue where the TS compiler keeps pointing out the first property and gives me the following error message on hover.

Type '{ gadgetID: string; gadgetName: string; gadgetModel: string; buildYear: string; gadgetOS: string; }' is not assignable to type 'Gadget'.
  Object literal may only specify known properties, and 'gadgetID' does not exist in type 'Gadget'.

Answer №1

Expanding on the response from @about14sheep The suitable Interface should be

export interface Device {
    deviceID: string;
    deviceName: string;
    deviceModel: string;
    manufactureYear: string;
    deviceOS: string;
}

Update (credit to @VLAZ): Your existing interface only declares a method named constructor - with no public attributes.

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

Determine the position of a nested object within a multidimensional array using JavaScript/TypeScript

My objective is to obtain the complete index of a complex object within a multidimensional array. For example, consider the following array: var arr = [ { name: "zero", children: null }, { name: "one", children: [ { ...

Preserving hyperlinks while transferring text from an HTML element using JavaScript

I'm struggling to preserve links from HTML elements while copying rich text, but have been unable to achieve it despite numerous attempts. The following code represents my best effort so far, however it only copies text without maintaining the links ...

How can Angular 2 e2e tests maintain respect for their execution order?

What's the best way to ensure that Angular 2 e2e tests run in the order they are declared? I am currently using Angular-cli for my project. ...

Encountered a ZoneAwareError while trying to incorporate angular2-onsenui into

Have you run the following commands in your terminal: npm install angular2-onsenui@latest --save npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0bfbea3b5bea5b990e2fee2fea8">[email protected]</a> ...

Specialized typescript function that is compatible with extended interfaces

Is there a way to create a versatile function that can be applied to any interface derived from a top-level interface? This function should take an unpersisted interface (without an id property) and return a persisted one (with an id property). The two ma ...

When incorporating HTML5 Canvas fonts into a PDF using jspdf, the text may appear blurry

I have developed a function that scales down the font size until the text width is smaller than the canvas width. This text is then added to a canvas containing a QR code. Subsequently, this canvas is included in a PDF file. The issue I am encountering i ...

Developing an Angular directive that accepts a function parameter using TypeScript

I've been attempting to integrate an Angular function parameter into a TypeScript directive, but it seems like my syntax is off. Here's an example of the directive code I'm working with: export class CustomDirective implements ng.IDirective ...

Tips for getting around the router in Angular 2

We have multiple template files containing their components, each with defined routes that can be called using the following syntax: <a routerLink="/Path-with-component"> Open page with component </a> In addition to these dynamic components, ...

Developing a custom camera system for a top-down RPG game using Javascript Canvas

What specific question do I have to ask now? My goal is to implement a "viewport" camera effect that will track the player without moving the background I am integrating websocket support and planning to render additional characters on the map - movement ...

I'm struggling to understand how to use rxjs 6 in conjunction with angular 6, specifically when it comes to utilizing interval

I'm struggling to update my rxjs code to version 6. Previously, I had the following code that would poll for new data every 5 seconds: import { Observable, interval } from 'rxjs'; import { switchMap, map } from 'rxjs/operators'; ...

Displaying messages in an Angular 2 chatbox from the bottom to the top

As a newcomer to using typescript, I am currently working on an angular 2 project and facing some challenges with creating a chatbox. My goal is to have new messages displayed at the bottom while pushing the old ones up one line at a time, as shown in this ...

Enhancing rxjs observables with pre- and post-actions (such as displaying or hiding a loading screen)

I'm attempting to create a function that wraps a given Observable and adds a loading screen to it. The function function addLoadingScreen<T>(obs$: Observable<T>): Observable<T> should function like this: Show the loading screen. E ...

Leveraging Material UI and TypeScript for Effective Styling: Maximizing the Use of !

Currently, I am in the process of developing a React application and incorporating Material UI for my components. One query that has arisen is regarding how to apply an !important property to a style. My attempt at achieving this looked like: <Paper cl ...

The drop down list does not support the 'click' function and is throwing a TypeError

Could someone please assist in troubleshooting my code? I am trying to select a specific value from a dropdown list but is encountering difficulties. The code is able to retrieve the values from the dropdown but is unable to successfully click on the mat ...

Tips for effectively utilizing tabs to showcase information for a particular user rather than solely the initial one

I am fetching multiple user data from an API call, and all users are rendered using the same HTML and CSS. However, when I switch between tabs for each user, only the first user's information changes instead of the specific user I clicked on. I have a ...

The input in the schema fails validation against the Angular 7 schema with the following data: {"name":"testng7"}

After updating the Angular CLI to the latest version on my Mac OS, I encountered an issue when trying to create a new project using the command ng new testng7. The error message displayed was: Schematic input does not validate against the Schema: {"na ...

Issues with modals appearing improperly after transitioning to NG-Bootstrap 15 and Bootstrap 5 upgrade

UPDATED following a thorough examination: In the process of upgrading our application from NG-Boostrap v11 with bootstrap 4 to NG-Boostrap v15 with Bootstrap 5 and Popper, we also made the switch from Angular 15 to Angular 16. Despite successfully resolvi ...

Troubleshooting error in Angular 5 with QuillJS: "Parchment issue - Quill unable to

I've been working with the primeng editor and everything seems fine with the editor itself. However, I've spent the last two days struggling to extend a standard block for a custom tag. The official documentation suggests using the quilljs API fo ...

Leveraging WorkerGlobalScope.importScripts() function in TypeScript

I'm currently developing a service-worker for (Angular) web-apps using Typescript. The idea is to have the service worker serve alongside a separate configuration.js file that can be used to customize it for each specific web-app. Since the configura ...

Angular: avoid API errors from being displayed in the HTML template

While working with HTTP requests in my Angular app, I encountered an issue where a status code of 400 is returned for invalid data input, causing the HTML template not to be displayed on the view. Currently, I am handling this situation by: this.myServic ...