I'm using Typescript.
I have a type defined like this:
export type SearchStoresParameters = {
storeCategory : string;
latitude: number;
longitude: number;
}
And I'm trying to convert above type object to a query string.
export const searchStores = async (searchStoresParameters : SearchStoresParameters ) => {
var queryString = Object.keys(searchStoresParameters).map((key) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(searchStoresParameters[key])
}).join('&');
const searchStoresApi = process.env.REACT_APP_BACKEND_SERVICE_API + "/stores?" + queryString;
const res = await fetch(
searchStoresApi,
);
const response = await res.json();
}
However, above code shows error :
(parameter) searchStoresParameters: SearchStoresParameters
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'SearchStoresParameters'.
No index signature with a parameter of type 'string' was found on type 'SearchStoresParameters'.ts(7053)
What's the best way to convert a typescript type to query string?