Reputation: 105
Is there a simple generic way in Typescript/Angular to see if a complete path to a property exists and has a value?
For example Could I have a function like
if (thisExists(aaa.bbb.ccc.ddd)) {
...
}
Where thisExists would determine somehow that it needs to check
if (aaa == null) return false
if (aaa.bbb == null) return false
etc I know from the html perspective there's a ?. operator, but is there something on the typescript side?
Upvotes: 0
Views: 141
Reputation: 5992
You can do something like this, which is called optional chaining
if (aaa?.bbb?.ccc?.ddd) {
Upvotes: 2