I have an interface FilterData
, which looks like the following:
export interface FilterData {
variables?: string[];
processDefinitionKey?: string;
}
In a request to the server, I receive the object filterSettings
which is of a type FilterData
, and I need to iterate over it.
This is what I'm doing right now:
for (const key in filterSettings) {
filterString += `${key}_eq_${filterSettings[key]},`;
}
But I receive the following error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'FilterData'. No index signature with a parameter of type 'string' was found on type 'FilterData'.ts(7053)
I read here that the interface doesn't exist on the runtime, but I have no idea about what workaround could be. How I can iterate over the interface object?
I have an interface FilterData
, which looks like the following:
export interface FilterData {
variables?: string[];
processDefinitionKey?: string;
}
In a request to the server, I receive the object filterSettings
which is of a type FilterData
, and I need to iterate over it.
This is what I'm doing right now:
for (const key in filterSettings) {
filterString += `${key}_eq_${filterSettings[key]},`;
}
But I receive the following error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'FilterData'. No index signature with a parameter of type 'string' was found on type 'FilterData'.ts(7053)
I read here that the interface doesn't exist on the runtime, but I have no idea about what workaround could be. How I can iterate over the interface object?
Share Improve this question asked Apr 15, 2020 at 10:52 KarenKaren 1,4295 gold badges27 silver badges50 bronze badges 3-
Can you please share your entire code? I don't think there's any problem with the code you posted here.
const key in filterSettings
, at runtime, does not useFilterData
but just the object properties offilterSettings
. It should work (and does in my little scratch file). – fjc Commented Apr 15, 2020 at 10:56 - i tried.. i didnt get error. did u try to update typescript ? – xdeepakv Commented Apr 15, 2020 at 11:09
- @xdeepakv You do get that error if your typescript config does not allow implicit any. – user13258211 Commented Apr 15, 2020 at 11:14
3 Answers
Reset to default 3You can just say to typescript that you're sure that key
is keyof Filterdata
:
for (const key in filterSettings) {
filterString += `${key}_eq_${filterSettings[key as keyof FilterData]},`;
}
You can either set noImplicitAny: false
in your tsconfig.json
It is true that interfaces dont exist at runtime, but you can still iterate over properties of an existing object from that interface.
Please see my answer here How to loop though Record<K, T> for ways to iterate through an object
Indeed, the interface doesn't exist at runtime. You have to hardcode the properties that you want - the object that implements the interface might have any other properties as well:
const filterSettings: FilterData;
for (const key of ['variables', 'processDefinitionKey']) {
filterString += `${key}_eq_${filterSettings[key]},`;
}