1

я написал код который из сервера может получить 2 типа массива но я получаю ошибку 23.25 lines I get two Errors The 'title' property does not exist for the type and the type argument,Я ожидаю, что строки 23 типа faceProductList [] и 25 типа faceProductList [] [] код

interface faceProductList {
  readonly title: string;
  readonly price: string;
  readonly prodState: string;
  readonly shipping: string;
  readonly sold: string;
  readonly src: string;
  readonly id: string;
  readonly to: string;
}

class Server {
    private url: string = 'https://foo0022.firebaseio.com/';
    public async request(id: string): Promise<(faceProductList[] | faceProductList)[]> {
        const res = await fetch(`${this.url}${id}`);
        const resArr: (faceProductList[] | faceProductList)[]  = await res.json();
        return resArr;
    }
    public async handler(id: string, valueSearch: string) { 
        await this.request(id)
        .then((array) => { 
            if(id){ 
              return  array.filter(({title}) => title.includes(valueSearch))
            }else{
                return [].concat(...array)
            }

            })
    }
}

1 ответ 1

0

Это потому что Вы написали 2 взаимоисключающих условия: либо faceProductList[] либо faceProductList[][]. Приходит либо масив либо массив массивов, во втором случае нет title и поэтому компилятор ругается. Оператор | не будет работь для разных типов интферфесов и их комбинаций, типа масив сущностей или сама сущность.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.