首页 > 解决方案 > 函数中带有字符串的打字稿条件总是返回 false

问题描述

我在函数的打字稿条件中有一个奇怪的错误。有我当前的代码。我的参数来自外部:

  getLevel(validation: string, status: string): string {
    let card = "";

    if (validation == "A") {
      if (!status) {
        card = "card-success";
      } else {
        switch (status) {
          case "S":
            // console.log("case s");
            card = "card-success";
            break;
          case "C":
          case "U":
          case "V":
            // console.log("case cuv");
            card = "card-low";
            break;
          default:
            // console.log("case default");
            card = "card-success";
            break;
        }
      }
    }
    return card;
  }

为了测试我试过这个:

  getLevel(validation2: string, status2: string): string {
    let card = "";
    const validation = "A"; // for test purpose
    const status = "U"; // for test purpose
    if (status == status2) { // for test purpose
      console.log("aeae"); // not working
    }

    console.log(status); //display U
    console.log(status2); // display U
    console.log(typeof status); //display string
    console.log(typeof status2); // display string

    if (validation == "A") {
      switch (status) {
        case "S":
          console.log("case s");
          card = "card-success";
          break;
        case "C":
        case "U":
        case "V":
          console.log("case cuv");
          card = "card-low";  // should display this one and works with variables defined within the function
          break;
        default:
          console.log("case default"); // display this one with data from function args
          card = "card-success";
          break;
      }
    }
    return card;
  }

因此,当我在代码中尝试使用变量时,它可以工作,但不能使用我的参数。我也尝试了一个简单的 if 语句,但它也不起作用。有我的调试器截图

在此处输入图像描述

标签: javascripttypescript

解决方案


你在声明const status = "U". 如果您将鼠标悬停status在 IDE 中,它会显示类型status"U". 这意味着该status变量只能是"U",不能是其他字符或字符串。如果你想手动对打字稿说 status 不应该是 type "U",你可以说:

const status : string = "U"

或者以一种更合乎逻辑的方式,声明您的status变量,let以便打字稿知道您可能会更改status变量的值。

let status = "U"


function getLevel(validation2: string, status2: string): string {
    let card = "";
    const validation = "A";
    let status = "U";
    if (status == status2) {
      console.log("aeae"); // not working
    }

    console.log(status); //display U
    console.log(status2); // display U
    console.log(typeof status); //display string
    console.log(typeof status2); // display string

    if (validation == "A") {
      switch (status) {
        case "S":
          console.log("case s");
          card = "card-success";
          break;
        case "C":
        case "U":
        case "V":
          console.log("case cuv");
          card = "card-low";  // should display this one and works with variables defined within the function
          break;
        default:
          console.log("case default"); // display this one with data from function args
          card = "card-success";
          break;
      }
    }
    return card;
  }

操场。


推荐阅读