Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect removal of parentheses when using an infer with a constraint in a function predicate #14279

Merged
merged 5 commits into from Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog_unreleased/typescript/14279.md
@@ -0,0 +1,16 @@
#### Fix parens in inferred function return types with `extends` (#14279 by @fisker)

<!-- prettier-ignore -->
```ts
// Input
type Foo<T> = T extends ((a) => a is infer R extends string) ? R : never;

// Prettier stable (First format)
type Foo<T> = T extends (a) => a is infer R extends string ? R : never;

// Prettier stable (Second format)
SyntaxError: '?' expected.

// Prettier main
type Foo<T> = T extends ((a) => a is infer R extends string) ? R : never;
```
24 changes: 16 additions & 8 deletions src/language-js/needs-parens.js
Expand Up @@ -466,22 +466,30 @@ function needsParens(path, options) {
}

case "TSConditionalType":
if (name === "extendsType" && parent.type === "TSConditionalType") {
return true;
}
// fallthrough
case "TSFunctionType":
case "TSConstructorType":
if (name === "extendsType" && parent.type === "TSConditionalType") {
const returnTypeAnnotation = (node.returnType || node.typeAnnotation)
.typeAnnotation;
if (node.type === "TSConditionalType") {
return true;
}

let { typeAnnotation } = node.returnType || node.typeAnnotation;

if (
typeAnnotation.type === "TSTypePredicate" &&
typeAnnotation.typeAnnotation
) {
typeAnnotation = typeAnnotation.typeAnnotation.typeAnnotation;
}

if (
returnTypeAnnotation.type === "TSInferType" &&
returnTypeAnnotation.typeParameter.constraint
typeAnnotation.type === "TSInferType" &&
typeAnnotation.typeParameter.constraint
) {
return true;
}
}

if (name === "checkType" && parent.type === "TSConditionalType") {
return true;
}
Expand Down
Expand Up @@ -278,20 +278,6 @@ type Unpacked<T> = T extends (infer U)[]
================================================================================
`;

exports[`issue-13275.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;

=====================================output=====================================
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;

================================================================================
`;

exports[`nested-in-condition.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down Expand Up @@ -431,3 +417,57 @@ type Unpacked<T> = T extends (infer U)[]

================================================================================
`;

exports[`parentheses.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
// #13275
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
type Foo<T> = T extends (new (...a: any[]) => infer R extends string) ? R : never;

// #14275
type Test<T> = T extends ((
token: TSESTree.Token
) => token is infer U extends TSESTree.Token)
Copy link
Member Author

@fisker fisker Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradzacher Is it possible to be longer/deeper?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can be any deeper. I'm not 100% certain though. You could probably nest more conditionals in place of the TSESTree.Token - would be worth playing to see what's allowed syntactically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems possible and I don't know how to fix, I'm going to pretend I don't know. ac1aac1

? U
: TSESTree.Token;
type Test<T> = T extends ((
token: TSESTree.Token
) => asserts token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;
type Test<T> = T extends (new (
token: TSESTree.Token
) => token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;

=====================================output=====================================
// #13275
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
type Foo<T> = T extends (new (...a: any[]) => infer R extends string)
? R
: never;

// #14275
type Test<T> = T extends ((
token: TSESTree.Token
) => token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;
type Test<T> = T extends ((
token: TSESTree.Token
) => asserts token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;
type Test<T> = T extends (new (
token: TSESTree.Token
) => token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;

================================================================================
`;
1 change: 0 additions & 1 deletion tests/format/typescript/conditional-types/issue-13275.ts

This file was deleted.

20 changes: 20 additions & 0 deletions tests/format/typescript/conditional-types/parentheses.ts
@@ -0,0 +1,20 @@
// #13275
type Foo<T> = T extends ((...a: any[]) => infer R extends string) ? R : never;
type Foo<T> = T extends (new (...a: any[]) => infer R extends string) ? R : never;

// #14275
type Test<T> = T extends ((
token: TSESTree.Token
) => token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;
type Test<T> = T extends ((
token: TSESTree.Token
) => asserts token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;
type Test<T> = T extends (new (
token: TSESTree.Token
) => token is infer U extends TSESTree.Token)
? U
: TSESTree.Token;