Skip to content

why the result of tuple to union using T[number] is disorder #42178

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

Closed
HomyeeKing opened this issue Jan 2, 2021 · 2 comments
Closed

why the result of tuple to union using T[number] is disorder #42178

HomyeeKing opened this issue Jan 2, 2021 · 2 comments

Comments

@HomyeeKing
Copy link

HomyeeKing commented Jan 2, 2021

version : ^4.0.3

you can find code here

or just look at this

type A = [1,2,3]
type B = A[number]

// expect ^= type B = 1 | 2 | 3
// output:  ^= type B = 3 |  1 | 2

I know it doesn't effect using, but I wonder the order mechanism

@ajafff
Copy link
Contributor

ajafff commented Jan 2, 2021

Types in unions are (most of the time) ordered in the order they were encountered when checking your code. This order is not guaranteed (see #17944 and linked issues) and may change in every compiler version or by changing your code just a tiny bit.

In your example 3 is encountered first as the length of the tuple, then the numeric literals 1, 2 and 3 again.
If you change your code a bit, you get a different order:

type A = [0,1,2,3]
type B = A[number] // 0 | 1 | 2 | 3

@HomyeeKing
Copy link
Author

@ajafff ok , thanks
I think it depend the algrithom behind the processor
we'd better give in to performance

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

No branches or pull requests

2 participants