F
F
fif2022-02-02 12:10:13
typescript
fif, 2022-02-02 12:10:13

How to pass through an object if it can have a different interface?

One of 4 objects can be passed to the method, so key can be one of the four interfaces. Tried to do so to pass through the object

createBlock(obj: contentBl | asideBl | EngineBl | PetsBl) {
    let key: keyof contentBl | asideBl | EngineBl | PetsBl;
    for (key in obj) {
        console.log(obj[key].name);
    }
}

But it gives me an error . The left operand of the for...in operator must be of type string or any.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2022-02-02
@fif

let key: keyof contentBl | keyof asideBl | keyof EngineBl | keyof PetsBl;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question