(TypeScript2) How do you execute an interface type loop?

When using a for loop, my let object is of type string, although the object I am repeating is of the type defined in the interface.

Below is the code I'm using. When I try to access map.attribute, which is defined on the interface as a string, I get an error. [The attribute property does not exist in the string type.]

I have the following interface and function:

interface IMapping {
    attribute: string;
    property: string;
}

mapAttributes(mappings: IMapping[], values) {            
    for (let mapping in mappings) {
        if (mapping.hasOwnProperty("attribute")) {
            console.log(this.attributes.find(attribute => attribute.name === mapping.attribute).value);
        }
    }
}

How should a for loop be defined so that I can use the property that was defined in my interface?

+4
source share
1 answer

I managed to run your example when replacing

for (let mapping in mappings) {

with

for (let mapping of mappings) {
+10
source

Source: https://habr.com/ru/post/1663841/


All Articles