microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.13k stars 12.5k forks source link

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

Open bluelovers opened 5 years ago

bluelovers commented 5 years ago

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:

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.

AnyhowStep commented 5 years ago

Related: https://github.com/microsoft/TypeScript/issues/29255

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

fatcerberus commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

Oh, right. I hecked up.

tp commented 5 years ago

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 commented 5 years ago

@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.

AnyhowStep commented 5 years ago

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 commented 5 years ago

@AnyhowStep good idea

sandersn commented 3 years ago

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.