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 ...
๐ŸŒ
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
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
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 - In TypeScript, the double question mark operator (??) can be used to assign a default value to a variable if it is null or undefined. This is useful when working with optional values or handling potential null values.
Top answer
1 of 15
432

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";
๐ŸŒ
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...
๐ŸŒ
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 ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Convex
convex.dev โ€บ typescript 101 โ€บ control flow & operators โ€บ 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:
๐ŸŒ
Angular Wiki
angularjswiki.com โ€บ angular โ€บ double-question-marks-or-nullish-coalescing-operator-in-angular-typescript
Double Question Marks(??) or Nullish coalescing Operator in Angular/Typescript | Angular Wiki
The syntax of nullish coalescing operator is very simple just use double question marks โ€˜??โ€™ after the variable followed by default value. ... You might be thinking that I will use OR operator โ€˜||โ€™ to assign default values.
๐ŸŒ
Danywalls
danywalls.com โ€บ avoid-check-null-or-undefined-in-typescript-using-optional-chaining-and-nullish-coalescing
Avoid Check Null or Undefined In Typescript using Optional Chaining and Nullish Coalescing | DanyWalls
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 ...
๐ŸŒ
Lightly-dev
lightly-dev.com โ€บ blog โ€บ typescript-double-question-mark
What is Double Question Mark (??) in TypeScript? - Lightly
When we use the double question mark operator (??) to assign a default value to displayName, it checks if name is null or undefined. Since name is null, the default value "Guest" is assigned to displayName. This allows us to provide a fallback value in case the variable is not defined or has ...
๐ŸŒ
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.
๐ŸŒ
Becomebetterprogrammer
becomebetterprogrammer.com โ€บ typescript-question-mark
Understanding the Question Mark (?:) in TypeScript - Become A Better Programmer
April 25, 2022 - Also, you can enable strictNullChecks compiler option to prevent assigning null values to a property that is an option, unless null is one of the type definitions of the property. I wrote other TypeScript articles, and I thought you might be interested in reading some of them since you are reading this. ... Share your thoughts by replying on Twitter of Become A Better Programmer or to personal my Twitter account. "What's the deal with all of those questions in TypeScript?" Me: Which questions? "Well, there are questions marks all over the code like this: function a (b?: string) {}" Me: Ohhh!
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
Syncfusion
syncfusion.com โ€บ blogs โ€บ javascript โ€บ use of the exclamation mark in typescript
Use of the Exclamation Mark in TypeScript | Syncfusion Blogs
December 6, 2022 - In TypeScript, the question mark is used to define an argument as optional. It is the same as specifying the type as undefined. It is similar to studentName: string | undefined. The above function will throw the following error.