error TS2678 Type "'String'" is not comparable to type '""' in angular 5

Issue

Here’s my code in angular 5:

gotToWallet(wallet) {

  const { countryId = '', currencyType = '' } = wallet || {};

  let walletIdentity;

  switch (currencyType) {
    case 'CRYPTO':
      walletIdentity = 'userWalletIdentity';
     break;
    case 'FIAT':
      walletIdentity = 'fiatWalletIdentity';
     break;
    case 'ERC20TOKEN':
      walletIdentity = 'xbxUserWalletIdentity';
     break;
 }

  const { currencyId = '' } = (wallet || {})[walletIdentity] || {};
  this.router.navigate([`walletMgmt/wallet-details/${currencyId}/${countryId}`]);

}

I am getting the following error when running ng build command:

ERROR in src/app/wallet-management/wallets/wallets.component.ts(202,12): error TS2678: Type '"CRYPTO"' is not comparable to type '""'.
src/app/wallet-management/wallets/wallets.component.ts(205,12): error TS2678: Type '"FIAT"' is not comparable to type '""'.
src/app/wallet-management/wallets/wallets.component.ts(208,12): error TS2678: Type '"ERC20TOKEN"' is not comparable to type '""'.

Why am i getting this error? the code seems to work fine when i run ng serve.
I am getting this error only when trying to make build.

Thanks, any help would be highly appreciated.

Solution

switch (currencyType as any) {

for some reason (maybe typescript version?), when you destruct currencyType: '' from wallet, it was interpreted by the compiler as type ‘””‘ instead of ‘string’. so cast it as any will do the trick

Answered By – Qiaosen Huang

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.