question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nullish coalescing doesn't work as expected in TypeScript

See original GitHub issue

Summary

Nullish coalescing doesn’t work as expected in TypeScript.

Steps To Reproduce:

  1. Write this bit of code in a TypeScript (.ts) file const hello = 'hi' ?? 'test'
  2. Add odd spacing for Prettier to fix
  3. Run the formatting shortcut in VS Code

Expected result

The badly formatted code should get formatted by Prettier.

Actual result

Nothing happens, the spacing stays odd and the Prettier plugin thinks there’s a syntax error. See the log below.

Additional information

Formatting nullish coalescing operators still seems to work perfectly in JavaScript files (.js), so this is only an issue with TS.

This issue has been found to not be an issue with Prettier itself but with this extension specifically: https://github.com/prettier/prettier/issues/7428#issuecomment-589012567

VS Code Version: 1.44.0

Prettier Extension Version: 4.2.0

OS and version: Windows 10 - Version 10.0.18363 Build 18363

Prettier Log Output

["ERROR" - 00:02:14] Error formatting document.
["ERROR" - 00:02:14] Expression expected. (1:29)
> 1 |         const hello = 'hi' ?? 'test'
    |                             ^
SyntaxError: Expression expected. (1:29)
> 1 |         const hello = 'hi' ?? 'test'
    |                             ^
	at e (c:\projects\DFE\node_modules\prettier\parser-typescript.js:1:329)
	at Object.parse (c:\projects\DFE\node_modules\prettier\parser-typescript.js:1:2261773)
	at Object.parse$2 [as parse] (c:\projects\DFE\node_modules\prettier\index.js:10629:19)
	at coreFormat (c:\projects\DFE\node_modules\prettier\index.js:13888:23)
	at format (c:\projects\DFE\node_modules\prettier\index.js:14146:73)
	at formatWithCursor (c:\projects\DFE\node_modules\prettier\index.js:14162:12)
	at c:\projects\DFE\node_modules\prettier\index.js:42756:15
	at Object.format (c:\projects\DFE\node_modules\prettier\index.js:42775:12)
	at c:\Users\Admin\.vscode\extensions\esbenp.prettier-vscode-4.2.0\dist\extension.js:1:250598
	at t.default.safeExecution (c:\Users\Admin\.vscode\extensions\esbenp.prettier-vscode-4.2.0\dist\extension.js:1:250898)
	at t.default.<anonymous> (c:\Users\Admin\.vscode\extensions\esbenp.prettier-vscode-4.2.0\dist\extension.js:1:250578)
	at Generator.next (<anonymous>)
	at s (c:\Users\Admin\.vscode\extensions\esbenp.prettier-vscode-4.2.0\dist\extension.js:1:244725)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Arrow7000commented, Apr 13, 2020

Hm you’re right, this issue only happens in one of my repos but not in another.

I was finally able to work out what was causing the issue. react-slick, a dependency I was using, had prettier as a dependency for some reason. It was using an old version of prettier and apparently VS Code prefers using the local version of prettier if there is one rather than the version that comes installed with the plugin.

Installing the latest prettier as a direct dependency solved this.

1reaction
creambyemutecommented, May 14, 2020

@Arrow7000 You are my hero 🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the nullish coalescing operator not work as a ...
My point is, when typescript reaches the point where a is passed to fs, I would expect it should be clear, that it...
Read more >
Nullish Coalescing: The ?? Operator in TypeScript
This behavior is why you shouldn't use the || operator to provide a fallback value for a nullable value. For falsy values, the...
Read more >
Nullish coalescing operator (??) - JavaScript - MDN Web Docs
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null ...
Read more >
Nullish coalescing operator '??' - The Modern JavaScript Tutorial
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first “defined” value of the two. We...
Read more >
Documentation - TypeScript 3.7
The nullish coalescing operator is another upcoming ECMAScript feature that ... doesn't need to be eagerly built out, TypeScript has no problem working...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found