Yes. As of TypeScript 3.7 (released on November 5, 2019), this feature is supported and is called Optional Chaining:

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.

Refer to the TypeScript 3.7 release notes for more details.


Prior to version 3.7, this was not supported in TypeScript, although it was requested as early as Issue #16 on the TypeScript repo (dating back to 2014).

As far as what to call this operator, there doesn't appear to be a consensus. In addition to "optional chaining" (which is also what it's called in JavaScript and Swift), there are a couple of other examples:

  • CoffeeScript refers to it as the existential operator (specifically, the "accessor variant" of the existential operator):

The accessor variant of the existential operator ?. can be used to soak up null references in a chain of properties. Use it instead of the dot accessor . in cases where the base value may be null or undefined.

  • C# calls this a null-conditional operator.

a null-conditional operator applies a member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

  • Kotlin refers to it as the safe call operator.

There are probably lots of other examples, too.

Answer from Donut on Stack Overflow
🌐
DEV Community
dev.to › danywalls › simplify-your-typescript-code-with-optional-chaining-and-nullish-coalescing-37on
Simplify Your Typescript Code with Optional Chaining and Nullish Coalescing - DEV Community
May 23, 2023 - The double question mark (??) checks if the left-hand side value is nullish (null or undefined) and provides the right-hand side value as the default if needed. Nullish coalescing ensures that we always have a valid jersey number, even if it's ...
🌐
Stack Overflow
stackoverflow.com › questions › 72271204 › using-questionmark-to-check-if-variable-is-null-or-undefined-bad-practice
typescript - Using questionmark (?) to check if variable is null or undefined - bad practice? - Stack Overflow
I heard from a colleague that it is bad practice to use a question mark to check if a variable is null or undefined and in the same turn call a property / method on this. For example. let text ...
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-3-7.html
TypeScript: Documentation - TypeScript 3.7
In TypeScript 3.7, this is identified as a likely error: ... This check is a breaking change, but for that reason the checks are very conservative. This error is only issued in if conditions, and it is not issued on optional properties, if strictNullChecks is off, or if the function is later called within the body of the if: ... If you intended to test the function without calling it, you can correct the definition of it to include undefined/null...
🌐
Medium
medium.com › @teamcode20233 › understanding-double-question-mark-operator-in-typescript-be869f210fe6
Understanding Double Question Mark Operator (??) in TypeScript | by Teamcode | Medium
July 3, 2023 - The double question mark operator, also known as the nullish coalescing operator, is a feature introduced in TypeScript to provide a concise way to handle null or undefined values.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
Top answer
1 of 15
428

Yes. As of TypeScript 3.7 (released on November 5, 2019), this feature is supported and is called Optional Chaining:

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.

Refer to the TypeScript 3.7 release notes for more details.


Prior to version 3.7, this was not supported in TypeScript, although it was requested as early as Issue #16 on the TypeScript repo (dating back to 2014).

As far as what to call this operator, there doesn't appear to be a consensus. In addition to "optional chaining" (which is also what it's called in JavaScript and Swift), there are a couple of other examples:

  • CoffeeScript refers to it as the existential operator (specifically, the "accessor variant" of the existential operator):

The accessor variant of the existential operator ?. can be used to soak up null references in a chain of properties. Use it instead of the dot accessor . in cases where the base value may be null or undefined.

  • C# calls this a null-conditional operator.

a null-conditional operator applies a member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

  • Kotlin refers to it as the safe call operator.

There are probably lots of other examples, too.

2 of 15
157

It is now possible, see answer of user "Donut".

Old answer: Standard JavaScript behaviour regarding boolean operators has something that may help. The boolean methods do not return true or false when comparing objects, but in case of OR the first value that is equal to true.

Not as nice as a single ?, but it works:

var thing = foo && foo.bar || null;

You can use as many && as you like:

var thing = foo && foo.bar && foo.bar.check && foo.bar.check.x || null;

Default values are also possible:

var name = person && person.name || "Unknown user";
🌐
Webdevtutor
webdevtutor.net › blog › typescript-null-check-with-question-mark
TypeScript Null Check with Question Mark
Next time you encounter a situation where you need to access properties of objects that might be null or undefined, remember to use the question mark operator for a smooth and reliable null check experience in TypeScript.
🌐
Omarileon
omarileon.me › blog › typescript-double-question-mark
mari. | Demystifying TypeScript's Double Question Mark: Nullish Coalescing
January 25, 2024 - Double question marks in TypeScript means the nullish coalescing operator. If the left-hand side operand is undefined or null, it returns the left-hand side, otherwise it returns the right-hand side operand · It's identical to the logical OR ...
Find elsewhere
🌐
Educative
educative.io › answers › how-to-use-a-question-mark-in-typescript-variables
How to use a question mark in TypeScript variables
The question mark ? in typescript is used in two ways: To mention that a particular variable is optional. To pre-check if a member variable is present for an object. It prevents errors related to undefined or null in a program.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-question-mark-for-null-check
Using the Question Mark in TypeScript for Null Checks
In TypeScript, the question mark ... a value assigned to them. To perform a null check using the question mark, you simply place it after the variable or property that you want to check....
🌐
Convex
convex.dev › typescript 101 › operators & control flow › question mark
Question Mark | TypeScript Guide by Convex
The && operator treats all falsy values as chain stoppers: 0, "", false, NaN, null, and undefined. Optional chaining only stops for null and undefined. Use optional chaining when you specifically want to handle missing values. Use && when you need to check for any falsy condition:
🌐
EDUCBA
educba.com › home › software development › software development tutorials › typescript tutorial › typescript question mark
TypeScript Question Mark | Complete Guide to TypeScript Question Mark
April 11, 2023 - As question mark marks the variable as optional, but any optional parameter should follow with required parameter. ... Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more. ... Function here requires any parameter to test if null or undefined. One of the most important point which has been resolved with TypeScript 3.7 version is continuously checking of variables or expressions for being null or undefined
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Webdevtutor
webdevtutor.net › blog › typescript-null-check-question-mark
Understanding TypeScript Null Check with Question Mark
You can chain multiple ? operators to perform deep null checks on nested properties: const postalCode = person.address?.postalCode; console.log(postalCode); // Output: undefined · By chaining the ? operator, TypeScript will gracefully handle the null or undefined values at each level of the object hierarchy.
🌐
Delft Stack
delftstack.com › home › howto › typescript › question mark in typescript
Question Mark Operator in TypeScript | Delft Stack
January 30, 2023 - The question mark or ? has relieved the users by defining optional parameters. Moreover, the ? operator can also act as a shorthand for checking if a certain attribute of an object is null or undefined.
🌐
GitConnected
levelup.gitconnected.com › when-to-use-the-question-mark-or-undefined-type-in-typescript-233e74ab436
When to use the question mark or undefined type in TypeScript | by Ian Spryn | Level Up Coding
January 17, 2023 - So what’s the difference? Simply put, the question mark means the attribute is optional. If you do not provide a value for the attribute, it will be undefined.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-question-mark-dot
What is the ?. operator (optional chaining) in TypeScript | bobbyhadz
The optional chaining (?.) operator can also be used to call a function if its value is not equal to null or undefined. ... Copied!function logger(callback?: (msg: string) => void) { callback?.('hello'); } logger(console.log); // 👉️ "hello" ... Trying to call the callback parameter would cause an error if it isn't provided, so we used the optional chaining (?.) operator to check if the reference is not null or undefined before calling the function.
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
The type checker previously considered null and undefined assignable to anything. Effectively, null and undefined were valid values of every type and it wasn’t possible to specifically exclude them (and therefore not possible to detect erroneous use of them). strictNullChecks switches to a new strict null checking mode.
🌐
Danywalls
danywalls.com › avoid-check-null-or-undefined-in-typescript-using-optional-chaining-and-nullish-coalescing
Optional Chaining, Nullish Coalescing: TypeScript
July 13, 2023 - The double question mark (??) checks if the left-hand side value is nullish (null or undefined) and provides the right-hand side value as the default if needed. Nullish coalescing ensures that we always have a valid jersey number, even if it's ...
🌐
Lightly-dev
lightly-dev.com › blog › typescript-double-question-mark
What is Double Question Mark (??) in TypeScript? - Lightly
The double question mark operator, also known as the nullish coalescing operator, is a feature introduced in TypeScript to provide a concise way to handle null or undefined values. It is represented by ??. When used, the double question mark operator checks if the value on the left-hand side ...