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

Typescript Quick Info Formatter Removes parentheses () from declared type on hovering, for conditionally inferring types #48729

Closed
ajitjha393 opened this issue Apr 16, 2022 · 5 comments · Fixed by #48836
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@ajitjha393
Copy link

Bug Report

Typescript Language Formatter function removes parentheses () from the declared type on hovering over it.
Not a bug with the Typescript parser itself.

🔎 Search Terms

infer, conditional typing, parentheses, hover

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about parentheses getting removed for conditionally inferring types

⏯ Playground Link

Playground link with relevant code

💻 Code

image

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

🙁 Actual behavior

Wrapped () gets removed on hovering from the declared conditional inferring type.
Because of this the hovered type shown is not the same as the declared type and they are not interchangeable and can cause bugs.

Without parenthesis, the TS parses it as a Type which returns a Function and the return type of the function is that conditional "void extends ..." which is not what was declared.

Example -
image

🙂 Expected behavior

() should be preserved and shown while hovering so to prevent the errors that can arise because of missing () in the conditional inferring types

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Apr 18, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 18, 2022
@RyanCavanaugh
Copy link
Member

It sounds like you might be writing a tool that's taking some incorrect assumptions on how this can be used? FWIW text returned by quick info is not guaranteed to be something you can paste into a file. For example, we might in the future use placedholder text that is not legal code.

@fatcerberus
Copy link

To be fair, as long as what the quick info is displaying is syntactically valid, it probably shouldn't change the behavior of the type, otherwise you're asking a human to parse valid code differently than the compiler would, which is confusing.

@ajitjha393
Copy link
Author

It sounds like you might be writing a tool that's taking some incorrect assumptions on how this can be used? FWIW text returned by quick info is not guaranteed to be something you can paste into a file. For example, we might in the future use placeholder text that is not legal code.

Actually, I was moving the type definition from one file to another, so while hovering over the conditionally inferring type Tail<T>
I noticed that wrapping parenthesis was not present, the rest were the same

Hence my obvious conclusion was maybe both are interchangeable so I tried removing it from my actual Tail definition and there wasn't even a syntax/type error

I tried variadic tuples and extracting Tail and then I was able to narrow down the error to this Missing parenthesis and how the TS compiler sees it differently

So I think as long as it is a valid TS syntax, quick info should resolve to the same or equivalent type!
Whereas in this case, the quick info type got resolved to never

@ajitjha393
Copy link
Author

@RyanCavanaugh can you guide me on where does this transformation from type to string code might be happening that's used by quickInfo or any other pointers I should look into?

Maybe I can try to investigate it and fix it.

@DanielRosenwasser
Copy link
Member

This was fixed by #48112, but thank you @Andarist for the test case!

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
4 participants