Linked Questions

Popular Questions

In the menucomponent.ts

    dishData: Dish[] = Dishes;

    selectedDish: Dish = Dishes[0] ;

Both the Dish[] and Dish are comming from the same file. I understand the first one that dishData is of type Dish array. Why one requires an angular bracket and the other doesn't. In the first one all the dishes data is being assigned to dishData and in the second one only the first dish (index 0) is being assigned to selectedDish. The dish type is array Dish[] so why this syntax selectedDish: Dish[] = Dishes[0]; displays an error "Dish is not assignable to type Dish[]. Property length is missing in type Dish". (vs code on hover selectedDish)

shared folder dishes.ts

    import { Dish } from './dish';

    export const Dishes: Dish[] = [
        {
            id: '0',
            name: 'Uthappizza',

Shared folder dish.ts

    import { Comment } from './comment';

        export class Dish {
            id: String;
            name: String;
            image: String;

menu.component.ts

    import { Dishes } from '../shared/dishes';
    import { Dish } from '../shared/dish';

    @Component({
      selector: 'app-menu',
      templateUrl: './menu.component.html',
      styleUrls: ['./menu.component.css']
    })
    export class MenuComponent implements OnInit {

      dishData: Dish[] = Dishes;
      selectedDish: Dish = Dishes[0] ;

      constructor() { }

      ngOnInit() {
      }

    }

Related Questions