You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
using { [key]: value } doesn't resolve type properly when using union types.Β #56157
/*Resolves to:{ x: string;};*/constx={['x'as'x']: '...'}/*Resolves to:{ [x: string]: string;}Instead of the expected:{ [x in 'x'|'y']: string}*/consty={['x'as'x'|'y']: '...'};
π Actual behavior
In the first example ts works as expected, resolving the key with type 'x' to have a type of 'x'.
But in the second example, ts resolves the key with type 'x'|'y' to have a type of string.
π Expected behavior
The expected behaviour was for the second example to work similarly to the first one, so that the key with type 'x'|'y' resolves to type 'x'|'y'.
Additional information about the issue
This has cause errors which seem to only be fixed with the as keyword - which I prefer not to use.
Activity
jcalz commentedon Oct 19, 2023
Duplicate of #13948, and note that
[K in 'x'|'y']: stringis equivalent to{x: string; y: string}and therefore not the type you actually want.typescript-bot commentedon Oct 22, 2023
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.