Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 433
Sushmit Sagar Avatar asked Dec 11 '22 03:12

Sushmit Sagar


1 Answers

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

like image 162
Qiaosen Huang Avatar answered Dec 12 '22 15:12

Qiaosen Huang