I am new to TypeScript and describe my scenario as follow:
I have a type like this:
type response = {
totalQuantity: number;
numberOfCatogory: number
appleQuantity: number;
bananaQuantity: number;
pearQuantity: number;
};
There is validation on each individual fruit quantity, in this case are appleQuantity, bananaQuantity, and pearQuantity, which means I need to get each individual fruit quantity from response.
So I created a string array which saves individual property key like this:
const individualParameters: string[] = ["appleQuantity", "bananaQuantity", "pearQuantity"]
Function logic like this:
for (const individualParameter of individualParameters) {
let individualQuantity = response[individualParameter];
...
}
But when I build, TSLint throws error at response[individualParameter] saying
Unsafe use of expression of type 'any'
I think it is because response can't recognize type from string array element?
I am new to TypeScript and curious of a good way to solve this problem.
individualQuantityandindividualCartCountResponseParameterare not declaredconst individualParameters= ["appleQuantity", "bananaQuantity", "pearQuantity"] as constinsteadconst individualParameters: string[] = ["appleQuantity", "bananaQuantity", "pearQuantity"]