ngOnChanges(changes: SimpleChanges) {
const {previousValue: prevDate, currentValue: currDate}: SimpleChange = changes.dateFilter;
}
In the above code snippet I want to specify type DateFilter
to the prevDate
and currDate
variables. How can I achieve that? I tried like <DateFilter>prevDate
and is not working.
You need to cast the type:
const prevDate: DateFilter = changes.dateFilte.previousValue as DateFilter
or:
const {previousValue: prevDate, currentValue: currDate} = changes.dateFilter as {previousValue: DateFilter; currentValue: DateFilter };
You can just type the parent object. Here you can see that types of object properties are inherited to destructored variables myFoo
and myBar
:
obj: Obj = { foo: 4, bar: '1'};
ngOnInit() {
const {foo: myFoo, bar: myBar} = this.obj;
if (myFoo === '3') {
// his condition will always return 'false' since the types 'number' and '"3"' have no overlap.
}
}
And with SimpleChanges something like:
ngOnChange(c: SimpleChanges) {
const obj: Obj = c['dateFilter'].currentValue;
const {foo: foo1, bar: bar1} = obj
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.