1
votes

I have created this helper to return VAT.

export const hasVAT = (country: string) => {
  const VATRates = {
   AT: 20, // Austria,AT
   BE: 21, // Belgium,BE
   BG: 19, // Bulgaria,BG
   CZ: 21, // Czech Republic,CZ
   CY: 19, // Cyprus,CY
   DK: 25, // Denmark,DK
   ...
 };
 return country in VATRates ? VATRates[country] : false;
};

But receiving this Type error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type ...'

any ideas?

1
What is the type of VATRates ? If you do const VATRates: {[keys: string]: number} = ... you should not get any error.acbay
lol, you are 100%. I was overlooking this. Thank youShawn Sheehan

1 Answers

1
votes

You can enforce typing on object keys, so your helper can be written as:

export const hasVAT = (country: string) => {
  const VATRates: {[key: string]: number} = {
   AT: 20, // Austria,AT
   BE: 21, // Belgium,BE
   BG: 19, // Bulgaria,BG
   CZ: 21, // Czech Republic,CZ
   CY: 19, // Cyprus,CY
   DK: 25, // Denmark,DK
 };

 return country in VATRates ? VATRates[country] : false;
};