Skip to content

Commit f2244bd

Browse files
Fix parens in inferred function return types with extends (#13289)
Co-authored-by: Gleb Dolzhikov <9276677@gmail.com>
1 parent 2d3a274 commit f2244bd

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#### Fix parens in inferred function return types with `extends` (#13289 by @GlebDolzhikov)
2+
3+
<!-- prettier-ignore -->
4+
```ts
5+
// Input
6+
type Foo<T> = T extends (...a: any[]) => (infer R extends string) ? R : never;
7+
8+
// Prettier stable
9+
type Foo<T> = T extends (...a: any[]) => infer R extends string ? R : never;
10+
11+
// Prettier main
12+
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
13+
```

src/language-js/needs-parens.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ function needsParens(path, options) {
412412
// fallthrough
413413
case "TSFunctionType":
414414
case "TSConstructorType":
415+
if (name === "extendsType" && parent.type === "TSConditionalType") {
416+
const returnTypeAnnotation = (node.returnType || node.typeAnnotation)
417+
.typeAnnotation;
418+
if (
419+
returnTypeAnnotation.type === "TSInferType" &&
420+
returnTypeAnnotation.typeParameter.constraint
421+
) {
422+
return true;
423+
}
424+
}
415425
if (name === "checkType" && parent.type === "TSConditionalType") {
416426
return true;
417427
}

tests/format/typescript/conditional-types/__snapshots__/jsfmt.spec.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,20 @@ type Unpacked<T> = T extends (infer U)[]
278278
================================================================================
279279
`;
280280
281+
exports[`issue-13275.ts format 1`] = `
282+
====================================options=====================================
283+
parsers: ["typescript"]
284+
printWidth: 80
285+
| printWidth
286+
=====================================input======================================
287+
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
288+
289+
=====================================output=====================================
290+
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
291+
292+
================================================================================
293+
`;
294+
281295
exports[`nested-in-condition.ts format 1`] = `
282296
====================================options=====================================
283297
parsers: ["typescript"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;

0 commit comments

Comments
 (0)