Repository: babel/babel (TypeScript)

Issue:
[Bug]:generate() modified parentheses in array with type assertions 
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

Programmatic API (`babel.transform`, `babel.parse`)

### Input code


```
import { parse } from "@babel/parser";
import generate from "@babel/generator";

const code = "onEdit([(point1 ?? point2) as Point, point3])";

const ast = parse(code, {
  plugins: ["typescript", "jsx"],
});

console.log(generate(ast, {}, code).code);
```

### Configuration file name

_No response_

### Configuration

```
  "devDependencies": {
    "@babel/generator": "^7.18.2",
    "@babel/parser": "^7.18.4",
    "@babel/traverse": "^7.18.2",
    "@babel/types": "^7.18.4",
    ....
  },
```

### Current and expected behavior

Actual
```
onEdit([(point1 ?? point2 as Point), point3])
```

Expected
```
onEdit([(point1 ?? point2) as Point, point3])
```

### Environment

  System:
    OS: macOS 12.2.1
  Binaries:
    Node: 18.12.1 - /usr/local/bin/node
    Yarn: 3.3.0 - ~/node_modules/.bin/yarn
    npm: 8.19.2 - /usr/local/bin/npm
  Monorepos:
    Yarn Workspaces: 3.3.0

### Possible solution

_No response_

### Additional context

_No response_


Hints:
Hey @mrgreenward! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.

Patch:
diff --git a/packages/babel-generator/src/node/parentheses.ts b/packages/babel-generator/src/node/parentheses.ts
--- a/packages/babel-generator/src/node/parentheses.ts
+++ b/packages/babel-generator/src/node/parentheses.ts
@@ -90,6 +90,14 @@ const enum CheckParam {
   forOfHead = 1 << 5,
 }
 
+function isTSTypeExpression(node: t.Node) {
+  return (
+    isTSAsExpression(node) ||
+    isTSSatisfiesExpression(node) ||
+    isTSTypeAssertion(node)
+  );
+}
+
 const isClassExtendsClause = (
   node: t.Node,
   parent: t.Node,
@@ -369,9 +377,7 @@ export function ConditionalExpression(
     isBinary(parent) ||
     isConditionalExpression(parent, { test: node }) ||
     isAwaitExpression(parent) ||
-    isTSTypeAssertion(parent) ||
-    isTSAsExpression(parent) ||
-    isTSSatisfiesExpression(parent)
+    isTSTypeExpression(parent)
   ) {
     return true;
   }
@@ -406,6 +412,7 @@ export function LogicalExpression(
   node: t.LogicalExpression,
   parent: t.Node,
 ): boolean {
+  if (isTSTypeExpression(parent)) return true;
   switch (node.operator) {
     case "||":
       if (!isLogicalExpression(parent)) return false;


Test patch:
diff --git a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js
--- a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js
+++ b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js
@@ -1,3 +1,6 @@
 (<T> x).y;
 (x as T).y;
 x!.y;
+
+(point1 ?? point2 as Point);
+(point1 ?? point2) as Point;
diff --git a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js
--- a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js
+++ b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js
@@ -1,3 +1,5 @@
 (<T> x).y;
 (x as T).y;
-x!.y;
\ No newline at end of file
+x!.y;
+point1 ?? (point2 as Point);
+((point1 ?? point2) as Point);
\ No newline at end of file
diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js
--- a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js
+++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js
@@ -1,4 +1,4 @@
 a => {
   var _a$c;
-  ((a !== null && a !== void 0 && a.b as any) && (_a$c = a.c) !== null && _a$c !== void 0 && _a$c.d as any) ? 0 : 1;
+  ((((a !== null && a !== void 0 && a.b) as any) && (_a$c = a.c) !== null && _a$c !== void 0 && _a$c.d) as any) ? 0 : 1;
 };