I read in this post that I could use fromObject in HttpParamsOptions to use an object as my query parameters on a query. Unfortunately I'm having some issues getting the Object to be in the right shape. I thought that it would be able to consume an object that had all strings for types and am not sure how to cast it into the right shape. I'd prefer not to have to iterate over object keys to get things to fit into this box - it loses all its value if I have to do that.
Can anyone explain to me what's going on here? I've read about the indexable types but that didn't help me figure out how to apply that to this case.
Type error reported:
TS2322: Type '{ fromObject: Query; }' is not assignable to type 'HttpParamsOptions'.
Types of property 'fromObject' are incompatible.
Type 'Query' is not assignable to type '{ [param: string]: string | string[]; }'.
Index signature is missing in type 'Query'.
Call site (started with {params: query} in the get and decomposed to this):
public retrieve(query: Query): Observable<PolicyRetrieveResult> {
const q: HttpParamsOptions = {fromObject: query};
const params = new HttpParams(q);
return this.http.get<Result>(url, {params: params});
}
My query class:
export class Query {
id: string;
source: string = 'constant';
type: string = 'otherConstant';
}
Here is HttpParamsOptions for reference:
export interface HttpParamsOptions {
/**
* String representation of the HTTP params in URL-query-string format. Mutually exclusive with
* `fromObject`.
*/
fromString?: string;
/** Object map of the HTTP params. Mutally exclusive with `fromString`. */
fromObject?: {
[param: string]: string | string[];
};
/** Encoding codec used to parse and serialize the params. */
encoder?: HttpParameterCodec;
}