Skip to content
CodingThailand's Blog
CodingThailand's Blog

by โค้ชเอก

  • Home
  • About Me
  • CodingThailand.com
CodingThailand's Blog

by โค้ชเอก

สรุป 5 Utility Types ที่ใช้บ่อยใน TypeScript

16/04/202516/04/2025

สำหรับมือใหม่ที่เริ่มเขียน TypeScript มาสักพักนึงอยากให้ลองศึกษา Utility Types ของ TypeScript เพิ่มเติมกันด้วยนะครับ ​:)

.

1. Partial<Type>

ทำให้ทุก property ทั้งหมดใน type เป็น optional

ตัวอย่าง:

interface Customer {

id: string

name: string

}

type PartialCustomer = Partial<Customer>

// ผลลัพธ์: { id?: string; name?: string }

อาจใช้ในกรณี request ตอนอัปเดตข้อมูลก็ได้ เช่น UpdateCustomerRequest เป็นต้น

.

2. Record<Keys, Type>

ช่วยสร้างชนิดข้อมูล object ที่ key ของ property เป็น Keys และ value ของ property เป็น Type ได้

ตัวอย่าง:

type Staff = “John” | “Bob”

const canPlayFootball: Record<Staff, boolean> = {

John: true,

Bob: false,

}

และยังใช้ร่วมกับ Partial<Record<…>> เมื่อไม่ต้องการ map ทุก key แต่ยังอยากได้ type safety

.

3. Omit<Type, Keys>

ลบ property บางตัว ออกจาก type ที่ต้องการ

ตัวอย่าง:

interface Product {

name: string

price: number

}

type ProductWithName = Omit<Product, “price”>

// ผลลัพธ์: { name: string }

.

4. ReturnType<Type>

ดึงชนิดข้อมูล (type) ของฟังก์ชันแบบ return ค่า

ตัวอย่าง:

function multiply(a: number, b: number): number {

return a * b

}

type Multiply(Return = ReturnType<typeof multiply>

// ผลลัพธ์: number

.

5. Readonly<Type>

ทำให้ property ทั้งหมดไม่สามารถถูกแก้ไขได้ (readonly)

ตัวอย่าง:

function resetScore(score: Readonly<Score>) {

score.count = 0 // จะ Error ตรงนี้

}

ใช้สำหรับให้รู้ว่าค่าจะต้องไม่ถูกเปลี่ยน ช่วยให้อ่านง่าย และป้องกันการแก้ไขโดยไม่ได้ตั้งใจ

.

สรุป การใช้ utility types จะช่วยให้โค้ดมีความยืดหยุ่น ปลอดภัย และลดความซับซ้อนได้ดีมาก

และโดยปกติ utility types จะทำงานแค่ระดับเดียว หากทุกคนต้องการรองรับแบบหลายระดับ (deep support) แนะนำ library ชื่อว่า ts-toolbelt https://github.com/millsp/ts-toolbelt นะครับ

Views: 8

Uncategorized

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Posts ล่าสุด

  • การใช้งาน Prefetching ใน Next.js
  • 14 ข้อคิด เพื่อการเป็น Developer ที่ดีขึ้น จาก “Lee Robinson”
  • เคยเจอ “TypeError: Cannot read property ‘x’ of undefined” หรือเปล่า
  • บันทึกการเขียนเว็บไซต์ใหม่ในรอบ 10 ปี ย้ายมาใช้ Next.js
  • ทำไมการจัดการ Error ใน JavaScript ถึงเป็นเรื่องท้าทาย?

Recent Comments

    หมวดหมู่

    • .NET
    • AI
    • Android
    • Angular
    • Angular 2
    • Coding
    • CSS
    • Database
    • Editor
    • Flutter
    • Git
    • HTML5
    • Ionic 2
    • Ionic 4
    • Ionic Framwork
    • JavaScript
    • Laravel
    • Laravel 5
    • Next.js
    • Node.js
    • PHP
    • PHP 7
    • Plugins
    • React
    • React Native
    • Template
    • Tools
    • TypeScript
    • UI
    • Uncategorized
    • Vue.js
    • XAMPP
    • Yii
    • คอร์สเรียน
    • แรงบันดาลใจ

    Archives

    • July 2025
    • April 2025
    • November 2024
    • October 2024
    • April 2020
    • February 2020
    • August 2019
    • September 2018
    • August 2018
    • February 2018
    • November 2017
    • October 2017
    • August 2017
    • July 2017
    • April 2017
    • October 2016
    • August 2016
    • May 2016

    Tags

    .NET android Angular Angular 2 Atom Coding Coding Standard CSS CSS 3 Datepicker Express.js extensions Git HTML HTML5 Ionic2 JavaScript Laravel5 laravel 5.5 MariaDB Material Design MySQL Node.js npm PHP PHP7 plugins PouchDB recaptcha Restful sail.js template typescript typscript XAMPP Yii2

    ผู้เยี่ยมชม

    • 0
    • 1,818,193
    • 547,568
    • 8
    ©2025 CodingThailand's Blog | WordPress Theme by SuperbThemes