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

Allow customization of type display format in hover tooltips #59370

Open
6 tasks done
lsby opened this issue Jul 20, 2024 · 4 comments
Open
6 tasks done

Allow customization of type display format in hover tooltips #59370

lsby opened this issue Jul 20, 2024 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@lsby
Copy link

lsby commented Jul 20, 2024

πŸ” Search Terms

  • customization type display
  • Array or T[]

βœ… Viability Checklist

⭐ Suggestion

In TypeScript, when hovering over a variable, the inferred type is displayed in a specific format.
For instance, an array of numbers is shown as number[].
However, some developers, including myself, prefer the alternative format Array<number> for readability and consistency with other generic types.

πŸ“ƒ Motivating Example

I have noticed that there is an WriteArrayAsGenericType flag in the Abstract Syntax Tree (AST) that can convert T[] to Array<T>.

I would like to request the addition of a configuration option in tsconfig.json to enable this flag, allowing developers to choose their preferred format for array types in hover tooltips.

For example:

{
  "compilerOptions": {
    "typeDisplayFormat": "generic" // Options could be "generic" for Array<number> or "shorthand" for number[]
  }
}

πŸ’» Use Cases

Complex Type Readability: I frequently write functional code that can lead to complex types like Task<Maybe<Task<Maybe<T[]>>[]>>[]. The shorthand [] notation can make it difficult to understand the nested generic relationships. Displaying these types as Array<T> would significantly improve readability and comprehension.

Consistency: Most other generic types use angle brackets (e.g., Promise<T>, Map<K, V>), and only array types use the special shorthand (number[]). Aligning array types with this convention would ensure consistency across all types and improve clarity.

@jakebailey
Copy link
Member

See also #59029

@lsby
Copy link
Author

lsby commented Jul 20, 2024

I found a temporary workaround that helped me with this issue.

I modified this file: C:\Users\<your_username>\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\node_modules\typescript\lib\typescript.js, and searched for WriteArrayAsGenericType. Here's the segment I found:

            if (context.flags & 2 /* WriteArrayAsGenericType */) {
              const typeArgumentNode = typeToTypeNodeHelper(typeArguments[0], context);
              return factory.createTypeReferenceNode(type2.target === globalArrayType ? "Array" : "ReadonlyArray", [typeArgumentNode]);
            }

I changed the condition context.flags & 2 to true. After restarting VSCode, it worked for me.

I believe this will stop working after a VSCode update, so it's just a temporary solution.

@Webmekanic
Copy link

@lsby can we collaborate to have a permanent solution for this?

@lsby
Copy link
Author

lsby commented Jul 21, 2024

@Webmekanic

I believe the permanent solution would be for the TypeScript team to add the configuration option we want, but there hasn't been a response so far.
Perhaps we could consider using a VSCode plugin to address this issue.
Do you have any other ideas?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants