【问题标题】:error TS7027: Unreachable code detected typescript错误 TS7027:检测到无法访问的代码打字稿
【发布时间】:2018-10-04 09:45:28
【问题描述】:
  sortArrayDate(arrayToSort, arrayDateKey, ascendingOrDecending) {
    if (true) {
      arrayToSort.sort(function(a, b){
        if (a[arrayDateKey] === '' || a[arrayDateKey] === null) {
          return 1;
        }
        if (b[arrayDateKey] === '' || b[arrayDateKey] === null) {
          return -1;
        }
        return new Date(a[arrayDateKey]).getTime() - new Date(b[arrayDateKey]).getTime();
   });
    } else {
        arrayToSort.sort(function(a, b){  //getting error
        if (a[arrayDateKey] === '' || a[arrayDateKey] === null) {
          return 1;
        }
        if (b[arrayDateKey] === '' || b[arrayDateKey] === null) {
           return -1;
        }
        return new Date(b[arrayDateKey]).getTime() - new Date(a[arrayDateKey]).getTime();
      });
    }
  }

我在提到的行上遇到了上述错误。代码有什么问题。我正在尝试从数组中排序日期。

【问题讨论】:

  • @bugs 它在标题中。
  • @Nancy,我想你想写 if(ascendingOrDecending) 而不是 if(true)

标签: angular typescript


【解决方案1】:

第二行函数的 if 条件为:

if(true)

然后你还有其他部分。由于第一个 if 将永远为真,否则永远无法到达/调用。这就是打字稿给出无法访问代码错误的原因。

如果你想让 if 里面的代码一直执行,你可以把它带出if/else 条件。

为了在不更改代码的情况下禁用此错误(不推荐),您可以使用tsconfig.json 更改编译器配置。添加这个来解除这个警告:

"allowUnreachableCode": true

【讨论】:

    【解决方案2】:

    编译器是对的,因为您使用if(true),所以else 分支上的任何语句都无法访问。您可以使用 "allowUnreachableCode": true 禁用此功能(尽管我不推荐)

    【讨论】:

      【解决方案3】:

      你的if(true) 说你总是进入那个 if 条件。因此无法到达 else 部分,因为您的 if 始终是 true

      【讨论】:

        【解决方案4】:

        打字稿编译器给出无法访问的代码错误,因为无法访问/调用 else 部分。我建议不要更改编译器配置 (tsconfig.json) 中的任何内容,而是使用始终导致布尔值 true 的条件来欺骗编译器。

        例如,将if(true) 替换为if(Math.random())

        Math.random() 的返回类型是number,使编译器认为它可以产生任何数字,包括zero,这可能会产生布尔值false,但实际上Math.random() 永远不会给出@ 987654330@ 并且总是一个正数,每次都会产生一个布尔值true

        这里是完整的代码:

          sortArrayDate(arrayToSort, arrayDateKey, ascendingOrDecending) {
            if(Math.random()) {
              arrayToSort.sort(function(a, b){
                if (a[arrayDateKey] === '' || a[arrayDateKey] === null) {
                  return 1;
                }
                if (b[arrayDateKey] === '' || b[arrayDateKey] === null) {
                  return -1;
                }
                return new Date(a[arrayDateKey]).getTime() - new Date(b[arrayDateKey]).getTime();
           });
            } else {
                arrayToSort.sort(function(a, b){  //no more error
                if (a[arrayDateKey] === '' || a[arrayDateKey] === null) {
                  return 1;
                }
                if (b[arrayDateKey] === '' || b[arrayDateKey] === null) {
                   return -1;
                }
                return new Date(b[arrayDateKey]).getTime() - new Date(a[arrayDateKey]).getTime();
              });
            }
          }
        

        【讨论】:

          【解决方案5】:

          else 部分仅在您的if 条件被评估为假时才处于活动状态。因此,如果您有类似 if(myVariable != null) 的内容,那么当您的元素 myVariablenull 时,您将到达 else 块,否则将执行 if 块。

          【讨论】:

            【解决方案6】:

            当文件从 .ts 重命名为 .tsx 时,在 react 中解决了这个错误。

            【讨论】:

            • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
            猜你喜欢
            • 2011-05-09
            • 2014-11-20
            • 1970-01-01
            • 2020-07-18
            • 2010-12-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-22
            相关资源
            最近更新 更多