函数中带有字符串的Typescript条件始终返回false

时间:2020-11-05 10:45:21

标签: javascript typescript

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

  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语句,但是它也不起作用。 这是我的调试器屏幕截图

enter image description here

4 个答案:

答案 0 :(得分:0)

您要声明const status = "U"。如果将鼠标悬停在IDE中的status上,则表示status的类型为"U"。这意味着status变量只能是"U",不能包含其他字符或字符串。如果您想手动对打字稿说状态不应为"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;
  }

Playground.

答案 1 :(得分:0)

我只是将原始的getLevel()方法复制并粘贴到测试项目中,并对其进行了调用,效果很好:

    test() {
    console.log(this.getLevel("A", null));
    console.log(this.getLevel("A", "S"));
    console.log(this.getLevel("A", "C"));
    console.log(this.getLevel("A", "U"));
    console.log(this.getLevel("A", "X"));
    console.log(this.getLevel("A", "UNKNOWN"));
}

输出是我期望的:

card-success
card-success
card-low
card-low
card-success
card-success

您的通话中必须有一个错误。

答案 2 :(得分:0)

我发现了为什么它不起作用:

我检查我的参数长度,字符串后有空格。

status.length // 2

所以我在变量中添加了.trim()

const trimedStatus = status.trim();

我想知道为什么我没有在Google控制台中看到它。

答案 3 :(得分:-1)

请检查您的变量声明一次。我已复制了工作示例。

您缺少 status2 变量声明。

function getLevel(validation, status) {
    let card = "";
    if (validation == status) { // for test purpose
      console.log("aeae"); // not working
    }

    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;
  }

getLevel('A','U');
getLevel('A','A');
getLevel('U','U');

相关问题