2

I have defined an inner class in my component to hold the view model and I need to convert the http response interface to this view model. I tried this way:

@Component({
  selector: 'app-tasklist-items-grid',
  templateUrl: './tasklist-items-grid.component.html',
  styleUrls: ['./tasklist-items-grid.component.scss'],
  providers: [TasklistItemsService, BsModalService]
})
export class TasklistItemsGridComponent implements OnInit {

// .....

  TaskListItemViewModel = class {    
    id?: number;
    tasklistId: number;
    typeOfTask: number;
    statusId: number;
    created_at: Date;
    updated_at: Date;
    checked: Boolean;
    rowVersion: number;
  }

  convertToViewModel = (item: TaskListItemViewModel) => item;
}

However, TaskListItemViewModel type is not found. I tried also defining both the inner class and the method as static but still didn't work.

What am I missing?

juliano.net
  • 7,982
  • 13
  • 70
  • 164

3 Answers3

0

you need to define the class outside of TasklistItemsGridComponent, with syntax like below but if you want to create nested classes you need to follow the solutions used in link: Any way to declare a nest class structure in typescript?

class TaskListItemViewModel { 
  id?: number;
  tasklistId: number;
  typeOfTask: number;
  statusId: number;
  created_at: Date;
  updated_at: Date;
  checked: Boolean;
  rowVersion: number;
  }

@Component({
  selector: 'app-tasklist-items-grid',
  templateUrl: './tasklist-items-grid.component.html',
  styleUrls: ['./tasklist-items-grid.component.scss'],
  providers: [TasklistItemsService, BsModalService]
  })
export class TasklistItemsGridComponent implements OnInit {
   convertToViewModel = (item: TaskListItemViewModel) => item;
}
Tomasz Błachut
  • 2,368
  • 1
  • 16
  • 23
Mia
  • 381
  • 1
  • 5
  • 15
0

I think you can declare interface of your model outside the component, and inside your component just create nested class (aka class expression) which implements that interface. And then you can add your methods inside the class expressions.

import { Component } from '@angular/core';

export interface MyInterface {
  id?: number;
  tasklistId: number;
  typeOfTask: number;
  // other properties
}
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  TaskListItemViewModel = class implements MyInterface {
    constructor(
      public id: number,
      public tasklistId: number,
      public typeOfTask: number
    ) {}
    // other methods which you want to implement
  }

  convertToViewModel = (item: MyInterface) => new this.TaskListItemViewModel(1, 2, 3);
}

Of course this example show - how powerful TypeScript is (in this example we see the Class expressions feature), but I wouldn't do that in real projects, since most popular styleguides recommend to declare classes/interfaces/enums... (aka models) in separate file, normally it's some-feature.model.ts. And then we can use our models in other parts of app like components, directives etc.

shohrukh
  • 2,989
  • 3
  • 23
  • 38
0

As per your requirement, you can use it as below:

@Component({
  selector: 'app-tasklist-items-grid',
  templateUrl: './tasklist-items-grid.component.html',
  styleUrls: ['./tasklist-items-grid.component.scss'],
  providers: [TasklistItemsService, BsModalService]
})
export class TasklistItemsGridComponent implements OnInit {
 taskListItem: TaskListItemViewModel;
 constructor() {
   // here or in ngOnInit
   // this.taskListItem = new TaskListItemViewModel();
 }
// .....
 ngOnInit() {
   // this.taskListItem = new TaskListItemViewModel();
 }
  convertToViewModel = (item: ITaskListItemViewModel) => item;
}

// in another file or same component you can write this class
export class TaskListItemViewModel implements ITaskListItemViewModel{    
    id?: number;
    tasklistId: number;
    typeOfTask: number;
    statusId: number;
    created_at: Date;
    updated_at: Date;
    checked: Boolean;
    rowVersion: number;
  }

interface ITaskListItemViewModel{    
        id: number;
        tasklistId: number;
        typeOfTask: number;
        statusId: number;
        created_at: Date;
        updated_at: Date;
        checked: Boolean;
        rowVersion: number;
      }
AlokeT
  • 1,086
  • 7
  • 21