Я хочу преобразовать результат выборки данных, чтобы он был готов к использованию компонентом.
Ответ возвращается как объект.
Мне нужно спроецировать свойство keywords
объекта, оно содержит массив объектов:
interface GetKeywordsResponse {
business : BusinessObj,
keywords : Array<KeywordObj>
}
- Для каждого объектав массиве я хочу проецировать свойство
keyword
interface KeywordObj {
id: number,
keyword: string
}
Предоставляя мне массив строк: //['foo', 'bar']
Я понимаю, что мне нужно pipe()
и map()
для достижения этой цели.Но мое решение - генерировать ошибки типа.
Метод обслуживания:
public getKeywords(): Observable<Array<string>> {
return this.http.get<Observable<GetKeywordsResponse>>(...url).pipe(
map((res: GetKeywordsResponse): Array<KeywordObj> => res.keywords),
map((res: KeywordObj): Array<string> => res.keyword )
}
Я ожидаю, что метод будет работать без проблем.Однако я сталкиваюсь с двумя ошибками:
1. Argument of type 'OperatorFunction<GetKeywordsResponse, KeywordObj[]>' is not assignable to parameter of type 'OperatorFunction<Observable<GetKeywordsResponse>, KeywordObj[]>'.
Type 'GetKeywordsResponse' is missing the following properties from type 'Observable<GetKeywordsResponse>': _isScalar, source, operator, lift, and 6 more.
2. Type 'string' is not assignable to type 'string[]'.
Как я могу исправить getKeywords ()?