ค้นหาเว็บไซต์

javascript - วิธีที่ดีกว่าในการตรวจสอบค่าที่ไม่เป็นเท็จใน JS/TS?


ฉันกำลังโพสต์เพื่อดูว่ามีใครมีวิธีที่สะอาดกว่านี้ในการตรวจสอบค่าที่ไม่ใช่ false ในไฟล์ JS/TS หรือไม่

ตลอดหลายปีที่ผ่านมา ฉันได้พยายามหลายครั้งในการเขียนคำสั่ง if ที่สะอาดขึ้นเพื่อตรวจสอบค่าที่ไม่เป็น falsy เพราะมันทำให้ฉันรำคาญที่พวกมันทั้งหมดทำสิ่งเดียวกันแต่ยาวพอสมควรและสุดท้ายก็ใช้พื้นที่ครึ่งหนึ่งของไฟล์โค้ดบางไฟล์

ฉันพยายามสร้างฟังก์ชันและพิมพ์พวกมันในหลายๆ วิธีเพื่อห่อหุ้มการบันทึกและจากนั้นโยนข้อผิดพลาด แต่ TS ไม่ยอมรับสิ่งนี้ว่าเป็นการตรวจสอบที่ปลอดภัย จนถึงตอนนี้ สิ่งเดียวที่ผ่านการตรวจสอบคือการใช้คำสั่ง if แบบมาตรฐาน พร้อมกับการโยนข้อผิดพลาดโดยตรงในบอดี้

ไม่มีวิธีที่ดีกว่าและไม่ซับซ้อนเกินไปในการจัดการการตรวจสอบซ้ำ ๆ เหล่านี้ในขณะที่ยังคงความปลอดภัยอยู่หรือ?

การตรวจสอบมาตรฐาน

 // Not falsy check
     if (!plan) {
        Log.log.stripe_webhook("Plan not found");
        throw new Error("Plan not found");
      }

  // Another not falsy check
      if (!currentUser.email) {
          Log.log.stripe_webhook("Current user does not have an email");
         throw new Error("Current user does not have an email");
       }

สิ่งที่ฉันได้ลอง

ฟังก์ชันช่วยเหลือ ฉันได้ลองใช้หลายตัวในช่วงเวลาต่าง ๆ แต่ตัวอย่างพื้นฐานนี้ TS ไม่ยอมรับ

// Function to ensure no false values. TS does not accept this as safe.
            function ensureNotFalsy(
              condition: any,
              errorMessage: string
            ): void {
              if (!condition) {
                Log.log.stripe_webhook(errorMessage);
                throw new Error(errorMessage);
              }
            }

            // Not falsy checks
            ensureNotFalsy(orgRef, "Organization reference not found");
            ensureNotFalsy(orgRef?.organization, "Organization not found");
            ensureNotFalsy(
              orgRef!.organization!.id,
              "Organization ID not found"
            );

  // ...Code below will still throw errors saying these checked values could be undefined.

การเพิ่มข้อผิดพลาดและการจัดการใน const ก็ไม่ทำงานเช่นกัน TS ดูเหมือนว่าต้องการให้ข้อผิดพลาดถูกโยนในแต่ละคำสั่ง if

 // Put logic in a const. TS doesn't recognize it as a safe.
        const throwError = (message: string) => {
            Log.log.stripe_webhook(message);
            throw new Error(message);
         };

          // Checks to ensure not falsy. Doesn't work with TS.
          if (!plan) throwError("Plan not found");
          if (!currentUser.email)
          throwError("Current user does not have an email");

  // ...Code below will still throw errors saying these checked values could be undefined.

ตัวอย่างภาพหน้าจอ

คำตอบ #1

ฟังก์ชันการยืนยันถูกออกแบบมาสำหรับวัตถุประสงค์นี้โดยเฉพาะ เพียงใช้ asserts condition แทน void สำหรับประเภทการคืนค่าของ ensureNotFalsy และมันควรจะทำงานได้

นี่คือตัวอย่าง:

function ensureNotFalsy(
  condition: any,
  errorMessage: string
): asserts condition {
  if (!condition) {
    throw new Error(errorMessage);
  }
}

const f = (orgRef: {organization?: {id?: string}} | null) => {
  ensureNotFalsy(orgRef, "Organization reference not found");
  ensureNotFalsy(orgRef.organization, "Organization not found");
  ensureNotFalsy(
    orgRef.organization.id,
    "Organization ID not found"
  );
}

ลิงก์สนามเด็กเล่น