I have an interface like this:
export interface BarsParams {
timeframe: string;
start?: string;
end?: string;
limit?: number;
page_token?: string;
adjustment?: Adjustment;
asof?: string;
feed?: string;
}
Then I have a function that takes as argment params: BarsParams
where I want to iterate through the key-value pairs of what I presumed was a regular object:
export function getBars(symbol: string, params: BarsParams) {
let url = new URL(`${symbol}/bars`, endpoints.STOCKS_URL.href).href;
for (const [key, value] of params) {
//do something
}
let body = params;
try {
const response = fetch(url, new RequestInit(Method.Get, body));
return response;
} catch (error) {
console.log(error);
}
}
In the function call I pass the search params like an "object" as follows:
getBars('someSymbol', {timeframe: '59Min', limit: 10}).then(...)
However, TypeScript throws:
Type 'BarsParams' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
Why does TypeScript not infer that this is an object with an iterator and how can it be solved conveniently?