🌐
GitHub
github.com › microsoft › TypeScript › issues › 26578
Nullish coalescing operator (??) · Issue #26578 · microsoft/TypeScript
June 27, 2018 - export interface Configuration { // Default: "(no name)"; empty string IS valid name?: string; // Default: -1; 0 is valid items?: number; // Default: true active?: boolean; } function configureSomething(config: Configuration) { // With null-coalescing operator config.name = config.name ??
Published   Aug 21, 2018
🌐
GitHub
github.com › microsoft › TypeScript › pull › 32883
nullish coalescing commit by Kingwl · Pull Request #32883 · microsoft/TypeScript
follow https://tc39.es/proposal-nullish-coalescing/ Introduction This document specifies the nullish coalescing operator ??. See the explainer for an introduction. The main design decisions made in this specification are: The right argument of ?? is evaluated only if needed ("short circuiting"). ?? has lower precedence than ||. ?? cannot immediately contain, or be contained within, an && or || operation.
Author   microsoft
🌐
GitHub
github.com › microsoft › TypeScript › issues › 48536
Nullish coalescing and conditional type narrowing · Issue #48536 · microsoft/TypeScript
December 20, 2021 - Came up in my code where I had two strings and each could be string | undefined and I wanted to nullish coalesce but only if one of them was not undefined. Had to use !
Published   Apr 03, 2022
🌐
TypeScript
typescriptlang.org › play › 3-7 › syntax-and-messaging › nullish-coalescing.ts.html
TypeScript: Playground Example - Nullish Coalescing
-1; config.active = config.active ?? true; // Current solution config.name = typeof config.name === "string" ? config.name : "(no name)"; config.items = typeof config.items === "number" ? config.items : -1; config.active = typeof config.active === "boolean" ? config.active : true; // Using || operator which could give bad data config.name = config.name || "(no name)"; // does not allow for "" input config.items = config.items || -1; // does not allow for 0 input config.active = config.active || true; // really bad, always true } // You can read more about nullish coalescing in the 3.7 blog post: https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
🌐
GitHub
github.com › microsoft › TypeScript › issues › 40613
Handling void by nullish coalescing operator · Issue #40613 · microsoft/TypeScript
July 10, 2020 - TypeScript Version: 4.0.2 Search Terms: nullish coalescing, void Code type PossiblyVoid = () => void | boolean; let empty : PossiblyVoid = () => { } let coalesced : boolean = empty() ?? true; Expected behavior: Should not throw a compila...
Published   Sep 17, 2020
🌐
GitHub
github.com › microsoft › TypeScript › issues › 62332
Nullish coalescing assignment operator does not narrow correctly on nested readonly string. · Issue #62332 · microsoft/TypeScript
February 24, 2025 - 🔎 Search Terms Nullish coalescing assignment operator 🕗 Version & Regression Information This happens to every version from v4.x to v6 nightly. ⏯ Playground Link https://tsplay.dev/wQaVnw 💻 Code const LabelLookup = { INNER_LABEL: 'INNER_...
Published   Aug 25, 2025
🌐
GitHub
github.com › microsoft › TypeScript › issues › 36393
Nullish coalescing should always include the type of the right operand · Issue #36393 · microsoft/TypeScript
microsoft / TypeScript Public · Notifications · You must be signed in to change notification settings · Fork 12.6k · Star 102k · New issueJump to bottomCopy link · New issueJump to bottomCopy link · Open · Open · Nullish coalescing should always include the type of the right operand#36393 ...
🌐
GitHub
github.com › TypeStrong › ts-loader › issues › 1061
Can't use nullish coalescing operator with TypeScript 3.8.2 · Issue #1061 · TypeStrong/ts-loader
December 12, 2019 - Expected Behaviour With TypeScript 3.8.2 I can't use the nullish coalescing operator ?? anymore. Actual Behaviour I'm getting the error message Module parse failed: Unexpected token (36:49) File was processed with these loaders: * ./node...
Published   Feb 25, 2020
Find elsewhere
🌐
GitHub
github.com › typescript-eslint › typescript-eslint › issues › 2642
[no-throw-literal] False positive with nullish coalescing in throw statement · Issue #2642 · typescript-eslint/typescript-eslint
No error is reported, as in this case the nullish coalescing operator is always returning something of type Error ... An error is reported by the @typescript-eslint/no-throw-literal rule: Expected an error object to be thrown
🌐
GitHub
github.com › microsoft › TypeScript › issues › 43705
Conditional type narrowing ignores nullish coalescing to falsy value · Issue #43705 · microsoft/TypeScript
It's an edge case with a good use case. @typescript-eslint/strict-boolean-expressions helps avoid tricky bugs by requiring code to separately handle nullish vs. falsy values. Nullish coalescing would be the simplest way to explicitly treat nullish values the same as an empty string.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 48473
Nullish coalescing assignment operator doesn't work correctly with logical operators · Issue #48473 · microsoft/TypeScript
January 12, 2022 - Using the nullish coalescing assignment operator ??= on a logical statement which contains a logical operator (&& or ||) fails to narrow the type, even though using the nullish coalescing operator ?? works correctly. declare function acceptBoolean (s: boolean): void declare let x: boolean | null, y: boolean | null; x = x ?? (4 < 3 && 3 < 4); acceptBoolean(x); // narrowed correctly y ??= 4 < 3 && 3 < 4; acceptBoolean(y); // error · Playground. TypeScript version 4.6.2 ·
Published   Mar 29, 2022
🌐
GitHub
github.com › microsoft › TypeScript › issues › 55013
Using nullish coalescing with untyped field in constructor trigger TS7022 · Issue #55013 · microsoft/TypeScript
Bug Report Using nullish coalescing in a constructor to initialize a class member with no type annotation will result in a TS7022 error. 🔎 Search Terms nullish, coalescing, 7022, ??, and ts7022 🕗 V...
🌐
GitHub
github.com › microsoft › TypeScript › issues › 58629
Nullish coalescing operator but for conditional types · Issue #58629 · microsoft/TypeScript
May 23, 2024 - 🔍 Search Terms nullish coalescing operator conditional types ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript code This could...
Published   May 23, 2024
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-3-7.html
TypeScript: Documentation - TypeScript 3.7
We owe a large thanks to community members Wenlu Wang and Titian Cernicova Dragomir for implementing this feature! For more details, check out their pull request and the nullish coalescing proposal repository.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 35798
Nullish coalescing for default function params · Issue #35798 · microsoft/TypeScript
function swatch(color ?? '#ddd') { // <- nullish coalescing of default param value console.log('rendering color:', color) } swatch(undefined); // rendering color: #ddd swatch(null); // rendering color: #ddd swatch(''); // rendering color: swatch('#bbb'); // rendering color: #bbb
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";
🌐
GitHub
github.com › microsoft › TypeScript › issues › 37872
Incorrect type inference with nullish coalescing operator · Issue #37872 · microsoft/TypeScript
TypeScript Version: 3.8.3, Nightly Search Terms: nullish coalescing operator, type inference Code (a: { a: {} | undefined}) => { const b = a?.a ?? ""; } Expected behavior: type of b should inferred as: {} | "" Actual behavior: type of b ...
🌐
Marius Schulz
mariusschulz.com › blog › nullish-coalescing-the-operator-in-typescript
Nullish Coalescing: The ?? Operator in TypeScript — Marius Schulz
August 14, 2021 - The value document.all is not considered to be strictly equal to either null or undefined, but it is considered to be loosely equal to both null and undefined. Because of this anomaly, the TypeScript compiler can't emit value != null as a check because it would produce incorrect results for document.all.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 41153
Logical nullish assignment together with nullish coalescing produces false positive for "use before assignment" · Issue #41153 · microsoft/TypeScript
April 1, 2020 - TypeScript Version: 4.1.0-dev.20201015 Search Terms: logical nullish assignment use before assigned Code let x: boolean; declare function getX(): boolean; declare function maybeGetX(): boolean | undefined; if (Math.random() > 0.5) { x = ...
Published   Oct 18, 2020