! is non-null assertion operator (post-fix expression) - it just saying to type checker that you're sure that a is not null or undefined.

the operation a! produces a value of the type of a with null and undefined excluded


Optional chaining finally made it to typescript (3.7)

The optional chaining operator ?. permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid. The ?. operator functions similarly to the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

Syntax:

obj?.prop // Accessing object's property
obj?.[expr] // Optional chaining with expressions
arr?.[index] // Array item access with optional chaining
func?.(args) // Optional chaining with function calls

Pay attention:

Optional chaining is not valid on the left-hand side of an assignment

const object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
Answer from Aleksey L. on Stack Overflow
Top answer
1 of 7
247

! is non-null assertion operator (post-fix expression) - it just saying to type checker that you're sure that a is not null or undefined.

the operation a! produces a value of the type of a with null and undefined excluded


Optional chaining finally made it to typescript (3.7)

The optional chaining operator ?. permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid. The ?. operator functions similarly to the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

Syntax:

obj?.prop // Accessing object's property
obj?.[expr] // Optional chaining with expressions
arr?.[index] // Array item access with optional chaining
func?.(args) // Optional chaining with function calls

Pay attention:

Optional chaining is not valid on the left-hand side of an assignment

const object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
2 of 7
164

Since TypeScript 3.7 was released you can use optional chaining now.

Property example:

let x = foo?.bar.baz();

This is equvalent to:

let x = (foo === null || foo === undefined)
  ? undefined
  : foo.bar.baz();

Moreover you can call:

Optional Call

function(otherFn: (par: string) => void) {
   otherFn?.("some value");
}

otherFn will be called only if otherFn won't be equal to null or undefined

Usage optional chaining in IF statement

This:

if (someObj && someObj.someProperty) {
  // ...
}

can be replaced now with this

if (someObj?.someProperty) {
  // ...
}

Ref: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html

🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-3-7.html
TypeScript: Documentation - TypeScript 3.7
At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null or undefined. The star of the show in optional chaining is the new ?. operator for optional property accesses.
Discussions

Suggestion: "safe navigation operator", i.e. x?.y
Current Status The TC39 proposal is now at stage 3 (🎉🎉🎉🎉🎉) Implementation is in progress You can expect this feature in TypeScript 3.7 We'll update here when it's available in a nightly bui... More on github.com
🌐 github.com
205
July 15, 2014
angular - Is there any Alternate of Safe Navigation Operator for typescript file for versions
I was Looking for alternate of safe navigation operator as my typescript version is 3.2. My code becomes very much lengthy if I have to check for 3 to 4 keys. Suppose I want to check for Obj.key1.k... More on stackoverflow.com
🌐 stackoverflow.com
Alternate syntax so I can have both a safe navigation and a monad unwrapping operator? - Programming Language Design and Implementation Stack Exchange
I'd like to have both a safe navigation operator and monad unwrapping operator. Safe navigation is like the feature in TypeScript: More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
May 17, 2023
apex - Safe Navigation Operator (?.) and integer comparisons - Salesforce Stack Exchange
Taking a step back, I'm not exactly sure what distinguishes an Integer that's null from a null that's null, but it seems the Safe Navigation Operator knows how to deal with these distinctions. More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com

! is non-null assertion operator (post-fix expression) - it just saying to type checker that you're sure that a is not null or undefined.

the operation a! produces a value of the type of a with null and undefined excluded


Optional chaining finally made it to typescript (3.7)

The optional chaining operator ?. permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid. The ?. operator functions similarly to the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

Syntax:

obj?.prop // Accessing object's property
obj?.[expr] // Optional chaining with expressions
arr?.[index] // Array item access with optional chaining
func?.(args) // Optional chaining with function calls

Pay attention:

Optional chaining is not valid on the left-hand side of an assignment

const object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
Answer from Aleksey L. on Stack Overflow
🌐
Reddit
reddit.com › r/typescript › is there a safe navigation operator for string indexes?
r/typescript on Reddit: Is there a safe navigation operator for string indexes?
November 2, 2022 -

If I try to look up a string index on a nullable object like obj['index'] I get an Object is possibly 'null' error. I know I can do obj && obj['index'] but this gets tiresome especially if you are following multiple levels deep.

obj?['index'] thinks I am trying to do a ternary operator. Is there some sort of way to sugar syntax this?

🌐
Wikipedia
en.wikipedia.org › wiki › Safe_navigation_operator
Safe navigation operator - Wikipedia
1 month ago - In programming languages where ... the safe navigation operator stops the evaluation of a method/field chain and returns null as the value of the chain expression. It was first used by Groovy 1.0 in 2007 and is currently supported in languages such as C#, Swift, TypeScript, Ruby, Kotlin, ...
🌐
Medium
medium.com › @zayani.zied › safe-navigation-operator-optional-chaining-js-and-angular-d253431a2625
Safe Navigation Operator-Optional Chaining (JS and Angular) | by Zied ZAYANI | Medium
February 20, 2023 - The safe navigation operator, also ... is a feature that allows us to safely access properties or functions of an object without throwing an error if any of the properties in the chain are nullish (null or undefined)....
🌐
DEV Community
dev.to › pssingh21 › safe-navigation-operator-bang-bang-bang-192j
Safe Navigation Operator? Bang! Bang Bang!! - DEV Community
March 12, 2021 - Safe navigation operator or optional chaining is now available in JavaScript and TypeScript >= v3.7🎉.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 16
Suggestion: "safe navigation operator", i.e. x?.y · Issue #16 · microsoft/TypeScript
July 15, 2014 - Current Status The TC39 proposal is now at stage 3 (🎉🎉🎉🎉🎉) Implementation is in progress You can expect this feature in TypeScript 3.7 We'll update here when it's available in a nightly bui...
Published   Jul 15, 2014
Find elsewhere
🌐
Beyondjava
beyondjava.net › elvis-operator-aka-safe-navigation-javascript-typescript
Elvis Operator (aka Safe Navigation) in JavaScript and TypeScript
March 5, 2018 - So it checks every access of an attribute or a method of the son attribute. If you forget to add the null, TypeScript faithfully reminds you. The internet has countless descriptions how to write your own safe navigation: Just write a get function and pass the path as string to it.
🌐
Silvercat
silvercat.com › blog › optional-chaining-vs-safe-navigation
JavaScript Optional Chaining vs Ruby Safe Navigation | Silvercat
December 2, 2022 - JavaScript (since ES2020) and TypeScript (since TypeScript 3.7) both support "optional chaining", whereby accessing a property on null or undefined does not error: undefined.foo // => Cannot read properties of undefined (reading 'foo') undefined?.foo // => undefined · This is a very similar operator to the Ruby "safe navigation" operator (added in 2.3.0) whereby calling a method on a nil receiver does not error:
🌐
Zied ZAYANI's Blog
zzayani.hashnode.dev › safe-navigation-operator-optional-chaining-js-and-angular
Safe Navigation Operator-Optional Chaining (JS and Angular)
February 27, 2023 - Because it’s important to ensure that your code is concise, efficient, and easy to read. It’s recommended to use the safe navigation operator (?.) instead of relying on ngIf to check if an object is null or undefined.
🌐
Medium
medium.com › @garfunkel61 › unwrapping-angulars-safe-navigation-operator-7641434aa29d
Unwrapping Angular’s Safe Navigation Operator | by Andy Dłubak | Medium
August 22, 2023 - Wouldn’t it be reassuring to know there’s water inside? In Angular, the safe navigation operator (aka the “Elvis operator”) acts as a reassurance. It’s a way of checking if there’s “water in the pool” before taking the leap.
Top answer
1 of 2
1

This feature, called "optional chaining" in MDN and other ECMAScript documentation, was moved to Stage 4 (ready for inclusion) in December 2019 and is published as part of ES2020. As of October 2021, according to caniuse.com, browser support is currently at 90.86% globally.

As described, and as discussed in its issue Microsoft/TypeScript#16, Typescript support for optional chaining was released on 5 November 2019—nearly two years ago at the time of this answer. Compilation solutions also exist in Webpack 5 or Babel's env for ES2020. As such, for most developers, it makes more sense to adopt modern versions of TypeScript tooling than to use an alternative implementation.

If you are strictly unable to use modern tooling, you'll need a helper method; as a language feature, optional chaining cannot be directly polyfilled. Compatible tested alternatives exist in a number of common libraries:

  • Lodash's _.get
  • Underscore's _.get
  • Closure's goog.object.getValueByKeys
2 of 2
-1

I made this function which can be used as alternate of safe navigation operator for typescript versions <3.7

/*
  This function is to validate if Object is accessible or not as well as returns its value if it is accessible.
  it will return false if Object is not accessible (if value is null or undefined)
  If Object is accessible then it will return its value.

  Example: if I want to check that is "obj.key1.key2" is accessible and I want to put check on its value.
  if (isAccessible(obj,["key1","key2"]) == some_value){
    ...do something...
  }

  no need to check for null and undefined for each key.

  NOTE: this function is alternate of "SAFE NAVIGATOR OPERATOR (?)" of typescript which is not supported in versions <3.7
*/

isAccessible(data, keys, start=0) {
  if (start == 0 && (data == null || data == undefined)) {
    console.warn("data",data);
    return data;
  } else {
    if (data[keys[start]] == null || data[keys[start]] == undefined) {
      console.warn("Object valid till", keys.slice(0,start),keys[start],"undefined");
      return data[keys[start]];
    } else {
      if (start + 1 >= keys.length) {
        // console.log("output",data[keys[start]]);
        return data[keys[start]];
      }
      return this.isAccessible(data[keys[start]], keys, start + 1);
    }
  }
}
🌐
Progress
docs.progress.com › bundle › openedge-oo-abl-develop-applications › page › Use-the-safe-navigation-operator.html
Use the safe navigation operator (?:)
February 14, 2022 - Skip to main contentSkip to search · Powered by Zoomin Software. For more details please contactZoomin
🌐
Salesforce Developers
developer.salesforce.com › docs › atlas.en-us.apexcode.meta › apexcode › langCon_apex_SafeNavigationOperator.htm
Safe Navigation Operator | Apex Developer Guide | Salesforce Developers
Use the safe navigation operator (?.) to replace explicit, sequential checks for null references. This operator short-circuits expressions that attempt to operate on a null value and returns null instead of throwing a NullPointerException.
🌐
NGCC in Angular Ivy
iq.js.org › questions › angular › what-is-safe-navigation-operator
What is safe navigation operator?
November 11, 2025 - The safe navigation operator(?)(or known as Elvis Operator) is used to guard against null and undefined values in property paths when you are not aware whether a path exists or not.
🌐
Howjavascriptworks
howjavascriptworks.com › home › demystifying the safe navigation operator in angular
Safe Navigation Operator Angular: Learn More
November 2, 2023 - This operator is like a guardian, ensuring you sidestep that brick. You’ll often see it in action as ‘object?.property’. Here, if the ‘object’ is null or undefined, Angular stops there, ensuring no errors are thrown. ... While it’s great in TypeScript, its real magic is evident in Angular templates. Ever had the pesky “Cannot read property ‘x’ of undefined” error? The Safe Navigation Operator acts as a shield against this.
🌐
DEV Community
dev.to › rogersantos › safe-navigation-operator-coming-to-js-ts-1fc7
Safe navigation operator coming to JS/TS - DEV Community
August 10, 2019 - TypeScript already helps by telling at development time if something can be null. That said, a language-level feature to help with that is really good! And we have good news! Just a few days ago, the Safe Navigator feature has gone to Stage 3, and to the TS 3.7.0 roadmap!