[TS] Suggestions don't show up for objects with an index signature
I am writing TypeScript with WebStorm. I have an object with a string index signature like the example below:
const STRINGS: { [key: string]: string } = {
modal_valueTh: 'Value TH',
modal_valueEn: 'Value EN',
};
When I open the suggestion box, it doesn't list all the properties in `STRINGS` object.
As soon as I delete the index signature, making my code like this:
const STRINGS = {
modal_valueTh: 'Value TH',
modal_valueEn: 'Value EN',
};
the suggestions then appear.
Is this a problem with WebStorm, auto completion or my code?
Please sign in to leave a comment.
Completion doesn't work in this case because TypeScript is statically typed, and the static type is
{ [key: string]: string }which obviously doesn't have any properties.When you add a type annotation like
: {[key: string]:string}, it gains a priority over the right part of assignment, so the right part is completely ignored, and variable type is just a type with a single indexer. This is how TypeScript works, and adding the indexer type ruins the static type checking.See https://stackoverflow.com/questions/52146544/why-autocomplete-stop-working-in-an-object-with-type-in-typescript/52157355#52157355