TypeScript type alias เทียบ interface



type alias

type ตั้งชื่อให้ type อะไรก็ได้:

type ID = string | number;
type Point = { x: number; y: number };
type Callback = (data: string) => void;
type StringArray = string[];
type Pair<T> = [T, T];
type Status = "active" | "inactive";

interface

interface ตั้งชื่อให้ object shape (รวม function, array ที่เป็น object):

interface Point {
  x: number;
  y: number;
}

interface Callback {
  (data: string): void;   // call signature
}

สำหรับ object — เหมือนกันเกือบหมด

// type
type User = {
  name: string;
  age: number;
};

// interface
interface User {
  name: string;
  age: number;
}

ทั้งสองใช้เป็น type ของตัวแปร/พารามิเตอร์ได้เหมือนกัน, extends/implements ได้, optional/readonly ได้

ความต่างที่สำคัญ

1. type ทำได้มากกว่า (ไม่จำกัดแค่ object)

type ID = string | number;              // union — type เท่านั้น
type Coords = [number, number];          // tuple
type Getter<T> = () => T;                // function alias
type Keys = keyof User;                  // type operator

// interface ทำ union / tuple / primitive alias ไม่ได้

2. interface merge ได้ (declaration merging)

interface Window {
  myGlobal: string;
}
interface Window {
  anotherGlobal: number;
}
// Window มีทั้ง myGlobal และ anotherGlobal — merge อัตโนมัติ

// type ทำแบบนี้ไม่ได้
type Foo = { a: string };
type Foo = { b: number };   // Error: Duplicate identifier 'Foo'

declaration merging มีประโยชน์เมื่อ extend type ของ library (เช่นเพิ่ม property ให้ Express Request — บทที่ 39) แต่ก็อาจทำให้เกิด merge โดยไม่ตั้งใจ

3. extends syntax ต่างกัน

// interface extends
interface Animal { name: string }
interface Dog extends Animal { breed: string }

// type intersection
type Animal = { name: string };
type Dog = Animal & { breed: string };

interface extends ให้ error message ที่ดีกว่าเมื่อ conflict; type & ยืดหยุ่นกว่า (intersect กับ union ได้)

4. performance (เล็กน้อย)

TS cache interface ได้ดีกว่าในบางกรณี — สำหรับ type ที่ใช้ซ้ำเยอะและ object shape ล้วน interface อาจ compile เร็วกว่านิดหน่อย (ไม่สำคัญสำหรับโปรเจกต์ทั่วไป)

5. error message

interface Box { value: string }
const b: Box = { value: 123 };
// Type 'number' is not assignable to type 'string'.  (ชี้ชัดว่า Box.value)

interface มักให้ error ที่อ้างชื่อ interface, type บางทีขยาย type ออกมาเต็ม ๆ (อ่านยากกว่าสำหรับ type ใหญ่)

แนวทางเลือกใช้

สถานการณ์ใช้
object shape / public API ของ libraryinterface
union, tuple, function type, primitive aliastype (บังคับ)
mapped type, conditional type, keyoftype (บังคับ)
ต้องการ declaration merging (extend lib types)interface
ประกอบ type จากหลายชิ้นด้วย & / |type

แนวทางที่ทีมส่วนใหญ่ใช้: interface สำหรับ object ที่เป็น “สิ่งของ” (User, Config, Props), type สำหรับทุกอย่างอื่น (union, function, utility) — แต่ ความสม่ำเสมอสำคัญกว่ากฎ เลือกอันหนึ่งเป็นหลักแล้วยึด

TypeScript Handbook แนะนำ: “ถ้าไม่แน่ใจ ใช้ interface จนกว่าจะต้องใช้ feature ของ type“

ตัวอย่างที่ต้องใช้ type

// union
type Result<T> = { ok: true; value: T } | { ok: false; error: string };

// mapped type
type Partial<T> = { [K in keyof T]?: T[K] };

// conditional type
type NonNull<T> = T extends null | undefined ? never : T;

// tuple
type RGB = [red: number, green: number, blue: number];

// function
type EventHandler = (event: Event) => void;

เรื่องที่คนมักพลาด

  • พยายาม type X = ... ซ้ำ — error (duplicate); interface merge, type ไม่
  • interface สำหรับ union — ทำไม่ได้; ใช้ type
  • กังวลว่าเลือกผิดแล้วโค้ดพัง — เปลี่ยนภายหลังง่าย (object shape แทบเหมือนกัน)
  • declaration merging โดยไม่ตั้งใจ — ประกาศ interface Options สองที่ในโปรเจกต์ → merge เงียบ ๆ; ตั้งชื่อให้เจาะจง
  • type แล้ว error message ยาว — สำหรับ object ใหญ่ interface อ่านง่ายกว่า
  • extends กับ type — ต้องใช้ & (intersection) แทน

สรุป

  • type = ชื่อให้ type อะไรก็ได้ (union, tuple, function, primitive, mapped, conditional)
  • interface = ชื่อให้ object shape — merge ได้ (declaration merging), error message มักดีกว่า
  • object shape: ทั้งคู่ทำได้เกือบเหมือนกัน — interface extends vs type &
  • ใช้ type เมื่อ: union / tuple / function alias / mapped / conditional / keyof
  • ใช้ interface เมื่อ: object “สิ่งของ” / public API / ต้องการ merge
  • ความสม่ำเสมอในทีม > กฎตายตัว; ไม่แน่ใจ → interface

บทถัดไปว่าด้วย intersection type และการประกอบ type — A & B, extend interface และ conflict