👨‍💻
มนศ.dev
  • หน้าแรก
  • คอร์ส
    • TypeScript 101
      • TypeScript คืออะไร?
      • TypeScript vs JavaScript
      • ติดตั้ง TypeScript
      • รู้จัก Basic Type ต่างๆ
      • การกำหนด Type ในฟังก์ชั่น
      • การสร้าง Interface
      • Literal Types
      • Type vs Interface
      • Union Types และ Intersection Types
      • Generics ขั้นพื้นฐาน
      • Optional Properties
      • Class (1)
      • Class (2)
      • ลองเขียนเว็บง่าย ๆ ด้วย TypeScript
      • Utility Types
    • Ruby ฉบับคนหัดโค้ด
      • บทนำ
      • ทำไมต้อง Ruby?
      • ลองเล่น Ruby
      • เตรียมพร้อมเรียน Ruby
      • ตัวเลข และชุดอักขระ
      • เมธอด
      • ตัวแปร
      • ตัวแปร (เฉลยแบบฝึกหัด)
      • ประเภทข้อมูลต่างๆ
      • Boolean
  • ลิงก์
    • Facebook Page
    • GitHub
    • monosor.com
    • วงแหวนเว็บ
Powered by GitBook
On this page

Was this helpful?

  1. คอร์ส
  2. TypeScript 101

Optional Properties

โดยปกติแล้วเมื่อเรากำหนด Interface หรือ Type (แบบ Object) แล้ว เมื่อนำไปใช้สร้าง Object ใดๆ ทุก Property จะเป็นแบบ Required คือต้องมีค่านั้นอยู่เสมอ เพื่อการันตีว่า Type จะเป็นตามที่กำหนดไว้เสมอ เช่น

interface User {
  username: string
  email: string
}

// หรือ
type User = {
  username: string
  email: string
}

let a: User = {
  username: "foo",
  email: "[email protected]",
}

let b: User = { // ❌ Type Error : ไม่ได้กำหนด Property 'email'
  username: "bar"
}

โค้ดนี้ถ้า Compile เป็น JavaScript ปกติก็จะยังใช้ได้ แต่ถ้าเราเรียก b.email จะได้ undefined แปลว่าโค้ดนี้จะไม่มีความ Type-safe แล้ว

ถ้าหากเราต้องการทำให้ Property ไม่เป็นแบบ Required ยกตัวอย่างเช่น การสร้าง User Account ใหม่ ไม่จำเป็นต้องกรอกเบอร์โทรศัพท์เสมอไป จะทำได้สองวิธี แบบแรกคือใช้ Union | กับ null

interface User {
  username: string
  phone: string | null
}

แต่วิธีนี้จะมีปัญหาว่า เรายังคงต้องกำหนดให้ phone เป็น null อยู่ดี

const user: User = { username: "foo", phone: "0876543210" } // ✅
const user: User = { username: "foo", phone: null }         // ✅
const user: User = { username: "foo" }                      // ❌

อีกวิธีหนึ่งคือเราต้องการให้ละ Property ไปเลย จะต้องใช้การกำหนดว่าเป็น Optional โดยใช้ตัว ? ด้านหลังชื่อ Property ดังนี้

interface User {
  username: string
  phone?: string   // ให้ phone เป็น Optional Property
}

const user: User = { username: "foo" } // ✅ ไม่ต้องมี phone ก็ใช้ได้

user.phone // undefined

โดยค่าของ phone จะเป็น Type ที่เรากำหนดให้ มา Union กับ undefined

PreviousGenerics ขั้นพื้นฐานNextClass (1)

Last updated 4 years ago

Was this helpful?

phone มีค่าเป็น string หรือ undefined