0
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.

4
  • Why not make the previousValue property of type DateFilter in SimpleChanges ? Commented Nov 14, 2019 at 6:25
  • @adiga SimpleChanges class is provided by angular, we don't have control over it Commented Nov 14, 2019 at 8:41
  • Isn't changes.dateFilter itself not of type DateFilter? prevDate and currentValue get their type from the destructured properties previousValue and currentValue belonging to changes.dateFilter. Commented Nov 14, 2019 at 10:06
  • @ford04 changes is of type SimpleChanges so changes.dateFilter becomes of type SimpleChange which has properties previousValue and currentValue. Both of them represent angular @Input() dateFilter: DateFilter. But in ngOnChanges() when fetched from changes.dateFilter.previousValue, the type is not set, we have explicitly set it. Which I can do in multi-line, but wanted to know if possible while de-structuring Commented Nov 14, 2019 at 12:04

1 Answer 1

1

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 };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.