I recently made changes to my interface and class structure:
export interface State {
arr : any[];
}
export const INITIAL_STATE: State = {
arr: []
};
This updated code compiles without any issues.
Now, I decided to modify the Interface to include a dynamic property:
export interface State {
arr : any[];
[key: string]: any
}
And for the class, here's how it looks now:
export const INITIAL_STATE: State = {
arr: [] ,
'a':2
};
- Still compiling successfully.
However, when I tried to make the type of dynamic properties stricter changing from [key: string]: any
to [key: string]: number
:
In other words:
export interface State {
arr : any[];
[key: string]: number
}
export const INITIAL_STATE: State = {
arr: [] ,
'a':2
};
An error occurred:
Error:(7, 14) TS2322: Type '{ arr: undefined[]; 'a': number; }' is not assignable to type 'State'. Property 'arr' is incompatible with index signature. Type 'undefined[]' is not assignable to type 'number'.
Question:
Why did this happen?
I'm struggling to comprehend the logic behind this restriction.
What steps can I take to resolve this issue?