Typescript ESLint, is it possible null check in global function and it pass ESLint

Viewed 40
  const isNotNullNumber = (e: number | undefined) => {
    if (e !== null && e !== undefined && e !== 0) {
      return true;
    }
    return false;
  };

  //pass eslint
  if (promotionBadgeData?.discountPercent) {
    icon1 = <Text>{`${promotionBadgeData?.discountPercent}%\nOff`}</Text>;
  }

  //pass eslint
  if (
    promotionBadgeData?.discountPercent !== null &&
    promotionBadgeData?.discountPercent !== undefined &&
    promotionBadgeData?.discountPercent !== 0
  ) {
    icon1 = <Text>{`${promotionBadgeData?.discountPercent}%\nOff`}</Text>;
  }

  //fail eslint
  if (isNotNullNumber(promotionBadgeData?.discountPercent)) {
    icon1 = <Text>{`${promotionBadgeData?.discountPercent}%\nOff`}</Text>;
  }

in this case, use the global function to null check and it will show

ESLint: Invalid type "number | undefined" of template literal expression.(@typescript-eslint/restrict-template-expressions)

so, is it possible use the global function to null check and pass the ESLint?

screen cap

0 Answers
Related