错误TS2678类型“'字符串'”与角度5中的类型“”“不可比

问题描述 投票:2回答:1

这是我在角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}`]);

}

运行ng build命令时出现以下错误:

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 '""'.

为什么我会收到此错误?当我运行ng serve时,代码似乎工作正常。我只是在尝试进行构建时遇到此错误。

谢谢,任何帮助将非常感谢。

javascript typescript switch-statement angular5
1个回答
3
投票

switch (currencyType as any) {

由于某种原因(也许是打字稿版本?),当你从钱包中破坏currencyType: ''时,它被编译器解释为类型'“”而不是'string'。所以任何人都会这样做

© www.soinside.com 2019 - 2024. All rights reserved.