Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type missing properties, but it doesn't?

I have two interfaces:

dish-component.ts:

    export interface DishComponent {
    componentId: string;
    componentQuantity: number;
}

dish.ts:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { DishComponent } from 'src/app/interfaces/dish-component';

export interface Dish extends DishComponent {
    name: string;
    components: DishComponent[];
    totalCost: number;
}

I’m using Angular, in my component for dishes I try to create a Dish:

dishes.component.ts:

import { Component } from '@angular/core';
import { Dish } from 'src/app/interfaces/dish';
// other imports

@Component({
  selector: 'app-dishes',
  templateUrl: './dishes.component.html',
  styleUrls: ['./dishes.component.css']
})

export class DishesComponent {

  // other stuff

  myDish: Dish = {
    name: "myTestDish",
    components: [
      {
        componentId: "sdfsdf76sdf76",
        componentQuantity: 4,
      },

      {
        componentId: "dfh097fgj46j",
        componentQuantity: 4,
      },
    ],
    totalCost: 2354,
  };

  // other stuff
}

myDish yields the error:

Type ‘{ name: string; components: { componentId: string; componentQuantity: number; }[]; totalCost: number; }’ is missing the following properties from type ‘Dish’: componentId, componentQuantity ts(2739)

I don’t understand, how is the properties componentId and componentQuantity missing? What am I doing wrong?

Probably something obvious I’m missing, but I’m glad if you can point it out.

Solution:

Remove the extends in dish.ts. The Dish extends DishComponent imply it needs to have all the properties DishComponent has.

>Solution :

If you remove the extends class u will see this is working as expected. Indeed DishComponent requires cmpId and cmpQt but u do not set it. U need or to specify or to remove which I assume will be the solution.

Extends is a keywoard that "copy" the following interface properties.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading