Answer the question
In order to leave comments, you need to log in
Why does defining an object property after defining a variable not work?
There is a code:
interface foo { [property: string]: number }
var bar: foo = { baz: 1 };
bar.bak = 1;
Answer the question
In order to leave comments, you need to log in
Maybe like this:
interface foo { [property: string]: number; bak?: number; }
var bar: foo = { baz: 1 };
bar.bak = 1;
The structure you are trying to describe is called a dictionary. It does not have such a concept as a property, there is a concept of an index. Accordingly, it is necessary to apply by index, i.e. like this:
If you absolutely need to access it as a property, specify the type as any:
var bar: any = { baz: 1 };
bar.bak = 1;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question