It's better to show code example than try to explain it:
type MyType = {[key: string]: string;}
let v: MyType = {
propOne: "one",
propTwo: "two"
}; // Why is there no error?
Why does the TS interpreter not throw an error here? As you see, the MyType
object should be an object with one property. But I could define the MyType
object with two properties.
In general: How is it possible to specify an allowable number of properties? (maybe using index signatures or some similar approach). To be more precise, how is it possible to typify such two objects:
let o1 = {
singlePropertyWithRandomName: "abcd"
};
let o2 = {
propertyWithRandomNameOne: "abcd",
propertyWithRandomNameTwo: "efgh",
...,
propertyWithRandomNameThousand: "xyz",
};