1

I have found this piece of code in on example:

const value: Token | undefined = object?.token

What this part "object?.token" actually means?

I cannot find it online, I am not sure how to search for it.

I understand that object.token is used to access token property of object, but what is the use of "?"

Thanks

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
Denis2310
  • 939
  • 1
  • 15
  • 41

1 Answers1

2

This is called Optional Chaining in Typescript.

Optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null or undefined.

For your example if object is not null and undefined get the value of property token.

See this

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42