Search code examples
arraysangularrxjsmodeltask

Angular problem with array constructor and oninit


src/app/views/tasks/tasks.component.ts:12:3 - error TS2564: Property 'tasks' has no initializer and is not definitely assigned in the constructor. 12 tasks: Task[];

export class TasksComponent implements OnInit {

  tasks: Task[];

  constructor(private dataHandler: DataHandlerService) {

  }

  ngOnInit() {
    this.dataHandler.tasksSubject.subscribe(tasks=>this.tasks = tasks);
  }
}

Solution

  • The compiler is repporting the error because tasks is not init and will only get a value once the subject has submitted a value.

    you probably should have :

    export class TasksComponent implements OnInit {
       tasks: Task[]|undefined;
    }
    

    To represent that tasks are undefined until the subscription.