TypeScript non-null assertion operator not stripped off with deep optional chaining

Bug Report

  • I would like to work on a fix!

I noticed a really strange regression in in @babel/core 7.12.7. When used with optional chaining, the TypeScript postfix “!” operator (non-null assertion) sometimes won’t get stripped off, resulting in a syntax error.

Input:

const a = 1;
const b = () => a?.b?.c!.d

Output with @babel/core@7.12.3:

const a = 1;

const b = () => {
  var _a$b;

  return a === null || a === void 0 ? void 0 : (_a$b = a.b) === null || _a$b === void 0 ? void 0 : _a$b.c.d;
};

Output with @babel/core@7.12.7:

const a = 1;

const b = () => {
  var _a$b;

  return a === null || a === void 0 ? void 0 : (_a$b = a.b) === null || _a$b === void 0 ? void 0 : _a$b.c!.d;
};

Notice the _a$b.c!.d at the end

Plugins used:

  • @babel/preset-typescript@7.12.7
  • @babel/plugin-proposal-optional-chaining@7.12.7 (via preset-env)

Author: Fantashit

2 thoughts on “TypeScript non-null assertion operator not stripped off with deep optional chaining

Comments are closed.