Создайте общий тип на const angular - PullRequest
0 голосов
/ 10 апреля 2019

У меня проблемы с обработчиками ошибок.Я пытаюсь создать универсальный retryPipeline в моем сервисе: при сбое вызова он повторяется 3 раза, прежде чем выдается ошибка.Все идет нормально.Это работает, если я помещаю код внутри метода, например так:

  getMun(id_del: number, id_muno: number): Observable<Mun> {

    let urlAPIMun = urlAPI;
    urlAPIMun += '/' + id_del + '/mun' + '/' + id_mun + '?flag_geometry=true';
    return this._http.get<Mun>(urlAPIMunicipios).pipe(
     //   tap(() => console.log('HTTP request executed')),
      retryWhen(errors => errors.pipe(
        // Concat map to keep the errors in order and make sure they
        // aren't executed in parallel
        concatMap((e: HttpErrorResponse, i) =>
          // Executes a conditional Observable depending on the result
          // of the first argument
          iif(
            () => i >= 3,
            // If the condition is true we throw the error (the last error)
            throwError(e.error.errores),
            // Otherwise we pipe this back into our stream and delay the retry
            of(e).pipe(delay(5000))
          )
        ))));
  }

Я пытаюсь извлечь код внутри канала, чтобы объявить const, а затем вызвать const в моем служебном вызове:

const RETRYPIPELINE =
  retryWhen((errors) =>
    errors.pipe(
      concatMap((e: HttpErrorResponse, i) =>
          () => i >= 3,
          throwError(e.error.errores),
          of(e).pipe(delay(5000))
        )
      )
    )
  );

return this._http.get<Mun>(urlAPIMunicipios).pipe(RETRYPIPELINE);

Но я получаю эту ошибку:

ошибка TS2322: тип "Observable <{}>" не может быть назначен типу "Observable".Типу «{}» не хватает следующих свойств из типа «Mun»: id_mun, id_del, den

Есть ли способ создать универсальный констант, который можно присвоить любому методу,хотя метод возвращает типизированное значение?Заранее спасибо

1 Ответ

0 голосов
/ 10 апреля 2019

Наконец, я исправил это благодаря этому ответу :

Добавление приведения в retryWhen, окончательно решило мою проблему:

export const RETRYPIPELINE =
  retryWhen<any>((errors) =>
    errors.pipe(
      // Use concat map to keep the errors in order and make sure they
      // aren't executed in parallel
      concatMap((e: HttpErrorResponse, i) =>
        // Executes a conditional Observable depending on the result
        // of the first argument
        iif(
          () => i >= 3,
          // If the condition is true we throw the error (the last error)
          throwError(e.error.errores),
          // Otherwise we pipe this back into our stream and delay the retry
          of(e).pipe(delay(5000))
        )
      )
    )
  )

;

...