Angular 2 SimpleChanges Object throws error at first npm start Angular 2 SimpleChanges Object throws error at first npm start node.js node.js

Angular 2 SimpleChanges Object throws error at first npm start


The accepted solution is suboptimal for TypeScript, as you're defeating the type system.

SimpleChanges does not have an entry property, so the compiler quite rightly balks. The solution is to treat the changes object as an array:

ngOnChanges(changes : SimpleChanges){  if (changes['entry']) {    this.service.getComments(changes['entry'].currentValue.id)  }}

Then you can continue to strongly type the ngOnChanges method.


To make the compiler not complain just change your method definition for parameter one from SimpleChanges to any:

ngOnChanges(changes: any) {//...


Maybe it's changed a lot now but this works these days

import {Component, Input, OnChanges, SimpleChanges} from '@angular/core';import {ConfigModel} from './config.model'@Component({    selector: 'selector',    templateUrl: './template.html',    styleUrls: ['./styles.scss']})export class BlaComponent implements OnChanges {    @Input() config: ConfigModel;    ngOnChanges(changes: SimpleChanges): void {        if (changes.config && changes.config.currentValue) {            let config = <ConfigModel>changes.config.currentValue;            // do more        }    }}

I myself got the compile error because i wasn't using .currentValue after calling changes.config