π Search Terms
"[key]", "key", "dynamic key"
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about the type inference of { [key]: value } objects.
β― Playground Link
https://www.typescriptlang.org/play?ts=5.3.0-beta#code/PQKgsAUASgpgzgewDYDd4AIAuCBckDek6x6AHjunJgE4CWAdgOYDckAvqxCMJAMYL0qZdAF50hCCXQBtAOSlZ6AIZx082QF0KsgHR7Z7SJFCRYiVBmx4IEqdPKUaDRlsd0mnNpACSgzDCUAE3QEADMsAAsYdBhSAAcYXn9A61sSe3QGNQUAH1kAT00KKndGQy4eCH4-dHzRcSJ09WVVdTzC1119dmYgA
π» Code
/*
Resolves to:
{
x: string;
};
*/
const x = {
['x' as 'x']: '...'
}
/*
Resolves to:
{
[x: string]: string;
}
Instead of the expected:
{
[x in 'x'|'y']: string
}
*/
const y = {
['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.
π Search Terms
"[key]", "key", "dynamic key"
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about the type inference of { [key]: value } objects.
β― Playground Link
https://www.typescriptlang.org/play?ts=5.3.0-beta#code/PQKgsAUASgpgzgewDYDd4AIAuCBckDek6x6AHjunJgE4CWAdgOYDckAvqxCMJAMYL0qZdAF50hCCXQBtAOSlZ6AIZx082QF0KsgHR7Z7SJFCRYiVBmx4IEqdPKUaDRlsd0mnNpACSgzDCUAE3QEADMsAAsYdBhSAAcYXn9A61sSe3QGNQUAH1kAT00KKndGQy4eCH4-dHzRcSJ09WVVdTzC1119dmYgA
π» Code
π 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 ofstring.π 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
askeyword - which I prefer not to use.