For the following code:
const x = {
a: 'c',
b: 'd'
};
const y = {
[x.a]: 'e',
}
the generated types are:
typeof x -> {
a: string,
b: string
}
typeof y -> {
[x: string]: string
}
Expected type:
typeof y -> {
c: string
}
Similar issue on SO has a solution which is not relevant here
Found the reported issue on Github which says this was fixed but is somehow not working
That's because typeof x.a
is actually a string. Here, x is constant but the value of x.a
can be changed to any string value.
If the value of x.a
is not going to change then a possible solution(using const assertion added in typescript version 3.4):
const x = {
a: 'c',
b: 'd'
} as const;
const y = {
[x.a]: 'e',
}
typeof y -> {
c: string
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With