错误 - 这种情况总是 return 'false' 因为类型 '"$value"' 和 '""' 没有重叠

Error - This condition will always return 'false' since the types '"$value"' and '""' have no overlap

我有如下代码:

describe("Dynamic testing fields validations", () => {
        it.each`
          field         | value             | expectedMessage
          ${"username"} | ${""}             | ${"username is not allowed to be empty"}
          ${"username"} | ${"b".repeat(4)}  | ${"username length must be at least 5 characters long"}
          ${"username"} | ${"b".repeat(21)} | ${"username length must be less than or equal to 20 characters long"}
          ${"email"}    | ${""}             | ${"email is not allowed to be empty"}
          ${"email"}    | ${"mailcom"}      | ${"email must be a valid email"}
          ${"password"} | ${""}             | ${"password is not allowed to be empty"}
        `(
          `return $expectedMessage when $field is ${"$value"} === "" ? "${"empty"}" : "$value" characters long`,
          async ({ field, expectedMessage, value }) => {
            user.username = value;
            user.password = value;
            user.email = value;

            const res = await exec();

            const {
              validationErrors: { details },
            } = res.body;

            const result = Object.assign({}, ...details);

            expect(result[field]).toBe(expectedMessage);
          }
        );
      });

我收到以下错误

This condition will always return 'false' since the types '"$value"' and '""' have no overlap when I do the conditional check in the ternary operator.

我什么都试过了,但没有成功。 好心人能告诉我如何解决这个问题以及为什么我会收到这个错误吗?

我认为你的问题来自于此

${"$value"} === "" ? "${"empty"}" : "$value"

从你的逻辑我可以这样理解

  • 如果你的值为空,你return${"empty"}(类似于你的"$value"

  • 如果你的值不为空,你也return "$value"

你可以这样简化逻辑

`return $expectedMessage when $field is "$value" characters long`