S
S
Sergey2017-03-28 13:51:58
JavaScript
Sergey, 2017-03-28 13:51:58

Is it possible to set a hybrid data type in ts?

export interface IRoomData {
    adults: number;
    children: number;
    childrenAges: number[]|{age: any}[];
}

How would I write that this is either an array of numbers or an array of objects?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2017-03-28
@Sergamers

export interface IRoomData {
    adults: number;
    children: number;
    childrenAges: number[]|{age: any}[];
}

if(this.roomsData){
          this.roomsData = this.roomsData.map((room) =>{
            for(let i = 0; i < room.childrenAges.length; i++){
              room.childrenAges[i] = {age: room.childrenAges[i]};
            }

            return room;            
          });
        }

I didn’t understand what it was connected with, but with such a record, ts stopped cursing. If anyone knows what was the reason would be glad to hear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question