Nullish coalescing doesn't work as expected in TypeScript
See original GitHub issueSummary
Nullish coalescing doesn’t work as expected in TypeScript.
Steps To Reproduce:
- Write this bit of code in a TypeScript (.ts) file
const hello = 'hi' ?? 'test'
- Add odd spacing for Prettier to fix
- 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:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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, hadprettier
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.
@Arrow7000 You are my hero 🥳