У меня есть Следующая html: < /p>
Код: Выделить всё
error code: {{ todos.error.errorCode }}
< /code>
и в моем коде TypeScript: < /p>
this.todos.subscribe((t) => {
const errorCode = t.error?.errorCode; // TypeScript error here
});
Код: Выделить всё
NG9: Property 'errorCode' does not exist on type 'Error'.
< /code>
пример кода < /strong> < /h4>
Вот полный воспроизводимый пример:
stackblitz < /p>
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { of, tap } from 'rxjs';
import { injectQuery } from '@ngneat/query';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
imports: [CommonModule],
template: `
error code: {{ todos.error.errorCode }}
`,
})
export class App {
#query = injectQuery();
todos = this.#query({
queryKey: ['todos'],
queryFn: () =>
of({ data: { status: 500 } }).pipe(
tap((response) => {
if (response.data.status === 500) {
throw { errorCode: response.data.status };
}
})
),
}).result$;
ngOnInit() {
this.todos.subscribe((t) => {
const errorCode = t.error?.errorCode; // TypeScript error here
});
}
}
bootstrapApplication(App);
< /code>
то, что я попробовал < /strong> < /h4>
[list]
[*] Я подтвердил, что ошибка действительно является объектом ({ errorCode: number }[/list]
Почему TypeScript обрабатывает T.Error как ошибка , и как я могу правильно его ввести, так что ErrorCode признан?
Подробнее здесь: https://stackoverflow.com/questions/794 ... -in-angula
Мобильная версия