Skip to content

[Feature request] sort literals in .d.ts #32224

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

Open
3 of 5 tasks
bluelovers opened this issue Jul 2, 2019 · 9 comments
Open
3 of 5 tasks

[Feature request] sort literals in .d.ts #32224

bluelovers opened this issue Jul 2, 2019 · 9 comments
Labels
Effort: Difficult Good luck. Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@bluelovers
Copy link
Contributor

bluelovers commented Jul 2, 2019

Search Terms

Suggestion

sort string literal in .d.ts

Use Cases

avoid string literal random change order when source code didn't change
and make code history is more easy read

in this issue thread string literal is mean => "a" | "c" | "b"

this not only happen on Record, it has chance happen at all type is auto create by typescript emit with string literal

Examples

"a" | "c" | "b" is create by typesctipt emit and some time will random change order

current .d.ts output


export declare const table_plus: Record<"a" | "c" | "b">
// "a" | "c" | "b" is random change order

in this request

sort ( use simple array.sort() ) it when output emit at .d.ts

export declare const table_plus: Record<"a" | "b" | "c">

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Maintainer's note [@sandersn]: To implementers: When you implement this, be sure to sort other literal types too. Consider sorting other things, like symbol, by the order of their string representation.

@sandersn sandersn added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Good First Issue Well scoped, documented and has the green light Suggestion An idea for TypeScript labels Jul 2, 2019
@AnyhowStep
Copy link
Contributor

Related: #29255

If you implement this, you'll break my compile-time tests again =(

@fatcerberus
Copy link

fatcerberus commented Jul 3, 2019

This is labeled good first issue, yet I seem to remember another issue talking about how union order isn’t guaranteed (it’s based on an internal ID number for each type) so that there was basically nothing that can be done about this without major architectural changes and/or performance issues...?

@sandersn sandersn added Experience Enhancement Noncontroversial enhancements and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 3, 2019
@sandersn
Copy link
Member

sandersn commented Jul 3, 2019

@fatcerberus This is just requesting that d.ts emit sorts the union right before printing it. It wouldn't affect the checker itself.
Should be fine, except for evil corner cases like the UnionToTuple type, which manage to export the internal ordering into another type.

@AnyhowStep but then it would be the last time???

@AnyhowStep
Copy link
Contributor

Oh, right. I hecked up.

@tp
Copy link

tp commented Aug 9, 2019

I see a consistently different sorting with the same TS version using tsc and tsc -w.

Would this also be fixed by this change, or should I write a separate report?

image

@sandersn
Copy link
Member

sandersn commented Aug 9, 2019

@tp It's almost certainly the same, since that difference is probably from types getting created in different order for tsc -w and getting assigned different type ids.

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 16, 2019
@AnyhowStep
Copy link
Contributor

AnyhowStep commented Aug 27, 2019

If we're sorting string literals, can we also sort all the other literal types?
number, bigint, boolean.

Maybe even unique symbols.
typeof b | typeof a should be typeof a | typeof b.

I just had another compile-time test break because the order of execution caused some union type to switch elements around.

image

@sandersn
Copy link
Member

@AnyhowStep good idea

@sandersn sandersn changed the title [Feature request] sort string literal in .d.ts [Feature request] sort literals in .d.ts Aug 27, 2019
bluelovers added a commit to bluelovers/ws-string that referenced this issue Jun 6, 2020
bluelovers added a commit to bluelovers/runes that referenced this issue Jun 20, 2020
@sandersn sandersn self-assigned this May 28, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 16, 2021
@sandersn
Copy link
Member

The fix I made, #44614, is too slow for big projects. I think the only way we could ship this would be to sort types on union creation.

@sandersn sandersn added Effort: Difficult Good luck. and removed Good First Issue Well scoped, documented and has the green light Fix Available A PR has been opened for this issue labels Jun 30, 2021
@sandersn sandersn removed their assignment Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Difficult Good luck. Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
7 participants