Open
Description
Bug Report
π Search Terms
spread computed property number reduce
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
β― Playground Link
Playground link with relevant code
π» Code
declare const id: number;
type Acc = { [key: string]: number };
declare const acc: Acc;
// Expected error, but got none. β
const acc2: Acc = {
...acc,
[id]: 'invalid',
};
If we change the type of the computed property id
from number
to string
, it works as expected (we get an error).
declare const id: string;
type Acc = { [key: string]: number };
declare const acc: Acc;
// Expected error, and got none. β
const acc2: Acc = {
...acc,
[id]: 'invalid',
};
Alternatively, if we remove the spread of acc
it works as expected:
declare const id: number;
type Acc = { [key: string]: number };
// declare const acc: Acc;
// Expected error, and got none. β
const acc2: Acc = {
[id]: 'invalid',
};
My workaround for now is to convert the type before it's used as a computed property:
declare const id: number;
type Acc = { [key: string]: number };
declare const acc: Acc;
// Expected error, and got none. β
const acc2: Acc = {
...acc,
[id.toString()]: 'invalid',
};
π Actual behavior
See code comments above.
π Expected behavior
See code comments above.