0

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?

1

1 Answer 1

2

You can do something like this, which is called optional chaining

if (aaa?.bbb?.ccc?.ddd) {
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kenny, this seems to be correct. I just have to figure out why vscode is flagging it as an error.
@landers are you using resharper? Because it sadly has no support for the latest typescript versions.

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.