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

Prettier 2.1.0 generates broken code with new ...infer T syntax in TypeScript 4.0 #9041

Closed
papb opened this issue Aug 25, 2020 · 7 comments · Fixed by #9044
Closed

Prettier 2.1.0 generates broken code with new ...infer T syntax in TypeScript 4.0 #9041

papb opened this issue Aug 25, 2020 · 7 comments · Fixed by #9044
Assignees
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program

Comments

@papb
Copy link

papb commented Aug 25, 2020

Prettier 2.1.0
Playground link

--parser typescript

Input:

type Tail<T extends any[]> = T extends [infer U, ...infer R] ? R : never;

Output:

type Tail<T extends any[]> = T extends [infer U, ...(infer R)] ? R : never;

Expected behavior:

Output should be identical to input. The output given by Prettier does not compile in TypeScript 4.0.

Extra information:

This was mentioned by @bradennapier in #8646 but was forgotten.

I am working around this with // prettier-ignore for now.


Thank you for this awesome code formatter!

@papb
Copy link
Author

papb commented Aug 25, 2020

By the way I am very surprised that TypeScript can't handle these parentheses... I feel the urge to put these parentheses myself, even if Prettier wouldn't put them...

Edit: I guess I just have to live with the fact that ...infer is actually another operator?

@fisker fisker added lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:bug Issues identifying ugly output, or a defect in the program labels Aug 25, 2020
@fisker fisker self-assigned this Aug 25, 2020
@bradennapier
Copy link

bradennapier commented Aug 25, 2020

I am not sure the exact semantics atm. It is actually quite possible that there are cases where ...(infer U) would be used in a valid way, I would have to play with it some more. There isn't actually any direct information there in the release notes.

I think you'd be able to do ...(infer U)[] but have to confirm and that would give you a less precise type clearly if it works, since it'd be a union rather than a tuple.

@alexander-akait
Copy link
Member

@bradennapier feel free to open a new issue

@bradennapier
Copy link

bradennapier commented Aug 25, 2020

@bradennapier feel free to open a new issue

Just replying to the message previous.

I'll have to play with the syntax when I can, I'm pretty sure what I said would work but on mobile atm.

It seems likely that prettier shouldn't correct either use of it -- or if the () is literally only valid when used as an array then it could be smarter and remove () unless followed by [].

If anyone else has time to test to confirm as well that'd be good.

The ...infer style is the new syntax though and worst case could be converted to the other style if needed in meantime easy enough :).

@thorn0
Copy link
Member

thorn0 commented Aug 25, 2020

@bradennapier You're overthinking it. ...(infer U)[] looks similar to ...(infer U), but it doesn't have the same structure in the AST.

@brodybits
Copy link
Contributor

I tried 2 more combos in tests/typescript/rest-type/infer-type.ts, don't know if they are relevant or not:

Input:

type Tail<T extends any[]> = T extends [infer U, ...infer R] ? R : never;
type Tail2<T extends any[]> = T extends [infer U, ...(infer R)] ? R : never;
type Tail3<T extends any[]> = T extends [infer U, ...(infer R)[]] ? R : never;

Output:

type Tail<T extends any[]> = T extends [infer U, ...infer R] ? R : never;
type Tail2<T extends any[]> = T extends [infer U, ...infer R] ? R : never;
type Tail3<T extends any[]> = T extends [infer U, ...(infer R)[]] ? R : never;

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Aug 25, 2020

@brodybits The definition of Tail2 currently results in:

:2:51 - error TS2574: A rest element type must be an array type. 

2 | type Tail2<T extends any[]> = T extends [unknown, ...(infer R)] ? R : never;
                                                      ~~~~~~~~~~~~

@fisker fisker mentioned this issue Aug 25, 2020
6 tasks
TkDodo added a commit to TkDodo/remeda that referenced this issue Sep 24, 2020
and re-format everything

this allowed me to get rid of the prettierignore, see also this issue: prettier/prettier#9041
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Nov 24, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants