S
S
Soridas2017-08-31 20:00:53
JavaScript
Soridas, 2017-08-31 20:00:53

Angular: how to check the data type of a component property?

"@angular/core": "^4.0.0"
"typescript": "~2.3.3"

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

  test: number = 123;
}

/*******/

class Test {
  name: string
}

@Component({
  selector: 'app-child',
  template: `<div>{{data.name}}</div>`,
})
export class ChildComponent {

  @Input() data: Test;
}

Why doesn't ChildComponent complain that the data property is not of the Test data type?
How can typescript or angular check the incoming properties of a component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexs0ff, 2017-09-04
@alexs0ff

The typescript compiler doesn't process component templates, so we won't get an error when compiling. As a workaround, I can suggest the following - convert data to properties and check the incoming type in the set method already at runtime. Or write your own decorator that will check incoming value types in a declarative style.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question