Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question