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

feat: sort unions #2502

Closed
Zamiell opened this issue Feb 13, 2024 · 2 comments
Closed

feat: sort unions #2502

Zamiell opened this issue Feb 13, 2024 · 2 comments
Labels
enhancement Improved functionality

Comments

@Zamiell
Copy link
Contributor

Zamiell commented Feb 13, 2024

Expected Behavior

Typedoc produces a webpage like this:

image

This union is bugged because it is in the wrong order (i.e. not sorted). This is because TypeDoc is just mirroring how TypeScript internally prints out the union.

Nathan Sanders explains that TypeScript does this internally because it builds the union data structure based on when the original types are first used. In other words, the type of 3 gets used first in the stdlib before the type of 1, so then are printed out in the order that the types are first added to the set.

Nathan also suggests that in principle this could be fixed in the TypeScript repo upstream by adding all of the types type A = 1; type B = 2; and so on in the first file of the stdlib so that they all get created in memory in order.

But for now, I propose that TypeDoc automatically sorts unions of numbers.

Steps to reproduce

Something like:

type Test = [1, 2, 3][number]
//   ^? - type Test = 1 | 2 | 3

(it works on twoslash but not on the playground because they use different stdlibs)

@Zamiell Zamiell added the bug Functionality does not match expectation label Feb 13, 2024
@Gerrit0 Gerrit0 added enhancement Improved functionality and removed bug Functionality does not match expectation labels Feb 14, 2024
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Feb 15, 2024

More design needed before this can be implemented.

What happens if it isn't just numbers?

  • Strings (how are template literals sorted?)
  • Symbols
  • Nested objects
  • Tuples
  • Intersections
  • Functions

@Zamiell
Copy link
Contributor Author

Zamiell commented Feb 16, 2024

  • to start with, i would only sort number literals, as it would be the least arbitrary/contentious sorting. (i think doing baby steps and iterating later is good!)
  • i don't have strong opinions about the sorting algorithm for the other things you specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improved functionality
Projects
None yet
Development

No branches or pull requests

2 participants