Leonhelm
Leonhelm

Reputation: 21

Angular: how to check the data type of component properties?

"@angular/core": "^4.0.0"

"typescript": "~2.3.3"

@Component({
  selector: 'app-parent',
  templateUrl: '<app-child [data]="test"></app-child>'
})
export class ParentComponent {

  test: number = 123;
}

/****/

class Test {
  name: string
}

@Component({
  selector: 'app-child',
  templateUrl: '<div></div>',
})
export class ChildComponent {

  @Input() data: Test;
}

Why does ChildComponent not swear that the data property does not belong to the Test data type? How can typescript or angular be able to check the incoming properties of a component?

Upvotes: 2

Views: 3065

Answers (1)

Rajez
Rajez

Reputation: 4067

You can use instanceof

if(data instanceof Test) {  console.log(true); }

Upvotes: 3

Related Questions