Thuta Learning
TypeScript
IntermediateProgrammingအလယ်အလတ်

Literal Types, as const & satisfies

TypeScriptသင်ခန်းစာ 1230 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Literal type နဲ့ literal union ("GET" | "POST") တွေကို ရေးပြီး widening ဘယ်အချိန် ဖြစ်လဲ ခန့်မှန်းနိုင်ဖို့
  • as const နဲ့ object/array တစ်ခုလုံးကို readonly literal type အဖြစ် ထိန်းထားနိုင်ဖို့
  • Type annotation, as assertion နဲ့ satisfies သုံးမျိုးရဲ့ ကွာခြားချက်ကို ရှင်းပြနိုင်ဖို့
  • as const array ကနေ (typeof ARR)[number] နဲ့ union type ထုတ်ယူနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

string type က "စာသား ဘာမဆို" လို့ ဆိုလိုပြီး literal type "GET" ကတော့ "GET ဆိုတဲ့ စာလုံး သုံးလုံး တိတိ" လို့ ဆိုလိုပါတယ်။ ဓာတ်လှေကားက ခလုတ်တွေလိုပါပဲ — 1, 2, 3 ထပ်ပဲ ရှိရင် "ထပ် 7" ကို နှိပ်လို့ မရပါဘူး။ "GET" | "POST" လို literal union က ခွင့်ပြုထားတဲ့ ခလုတ်တွေကို စာရင်းပြုစုထားတာပါ။

ပြဿနာက TypeScript က literal တွေကို မကြာခဏ string လို ကျယ်တဲ့ type အဖြစ် ချဲ့လိုက်တာပါ။ as const က "ဒီ value တွေကို ရေးထားတဲ့အတိုင်း တိတိကျကျ မှတ်ထား" လို့ ပြောပြီး satisfies က "ဒီ value က ဒီ shape နဲ့ ကိုက်လား စစ်ပေး၊ ဒါပေမယ့် type ကို မပြောင်းနဲ့" လို့ ပြောပါတယ်။

Problem → Why → Solution

ပြဿနာ: annotation က key တွေကို ဖျောက်ပစ်တယ်

App ရဲ့ route config ကို Record<string, Route> လို့ annotate လုပ်ထားတယ် ဆိုပါစို့။ Shape ကို စစ်ပေးတာ မှန်ပေမယ့် annotation က variable ရဲ့ type ကို Record<string, Route> အဖြစ် အစားထိုးလိုက်လို့ "home" နဲ့ "account" ဆိုတဲ့ key တွေ ရှိမှန်း compiler မသိတော့ပါဘူး။ routes.acount လို စာလုံးပေါင်းမှားတာကို error မပြဘဲ undefined ပဲ ပြန်လာပြီး path ကလည်း "/account" မဟုတ်တော့ဘဲ string ပဲ ဖြစ်သွားပါတယ်။

routes.ts
type Route = { path: string; secure: boolean };

const routes: Record<string, Route> = {
  home: { path: "/", secure: false },
  account: { path: "/account", secure: true },
};

const link = routes.acount.path; // no compile error: any string key is allowed
// At runtime: TypeError, cannot read properties of undefined (reading 'path')

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

Type annotation က "ဒီ variable ရဲ့ type က ဒါပဲ" လို့ ကြေညာတာဖြစ်လို့ value ထဲက ပိုတိကျတဲ့ အချက်အလက်တွေကို ဖယ်ထုတ်ပစ်ပါတယ်။ Annotation ကို ဖြုတ်လိုက်ရင်တော့ key တွေ ပြန်ရပေမယ့် shape ကို ဘယ်သူမှ မစစ်တော့ပါဘူး။ satisfies က ဒီ ရွေးချယ်မှု နှစ်ခုကြားက ပြဿနာကို ဖြေရှင်းပေးပါတယ် — စစ်လည်း စစ်ပြီး တိကျတဲ့ type ကိုလည်း ထိန်းထားပါတယ်။

Core concept: widening, as const နဲ့ satisfies

Literal type ဆိုတာ value တစ်ခုတည်းကိုပဲ ခွင့်ပြုတဲ့ type ပါ — "GET", 42, true စတာတွေပါ။ ဒါတွေကို union နဲ့ ပေါင်းလိုက်ရင် type Method = "GET" | "POST"; လို ရွေးချယ်စရာ စာရင်း ရပြီး "DELETE" ဒါမှမဟုတ် "get" လို စာလုံးပေါင်းမှားတာကို compiler က ဖမ်းပေးပါတယ်။ Number literal တွေလည်း ဒီအတိုင်းပါပဲ — type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6; က 7 နဲ့ 0 ကို ငြင်းပါတယ်။ တကယ်တော့ boolean ကိုယ်တိုင်က true | false ဆိုတဲ့ union တစ်ခုပါပဲ။

TypeScript က value တစ်ခုရဲ့ type ကို infer လုပ်တဲ့အခါ နောက်ပိုင်း ပြောင်းနိုင်မလား ကြည့်ပါတယ်။ const exact = "GET"; ကို ဘယ်တော့မှ ပြန် assign လုပ်လို့ မရလို့ type က "GET" ပါ။ let loose = "GET"; ကတော့ နောက်မှ တခြား string ထည့်နိုင်လို့ type ကို string အဖြစ် ချဲ့လိုက်ပါတယ် — ဒါကို widening လို့ ခေါ်ပါတယ်။ Object property တွေလည်း ပြောင်းလို့ရတဲ့အတွက် const request = { method: "GET" }; ထဲက method ရဲ့ type က string ဖြစ်သွားပြီး Method လိုအပ်တဲ့ function ဆီ ပို့ရင် error တက်ပါတယ်။

as const က widening ကို အဆင့်တိုင်းမှာ ပိတ်ပါတယ်။ Object နဲ့ array ထဲက value အားလုံးကို literal type အဖြစ် ထားပြီး property တိုင်းကို readonly လုပ်ကာ array ကို readonly tuple အဖြစ် ပြောင်းပါတယ်။ { method: "GET" } as const ရဲ့ type က { readonly method: "GET" } ဖြစ်ပါတယ်။ as const က compile time မှာပဲ အလုပ်လုပ်ပြီး JavaScript code တစ်ကြောင်းမှ မထုတ်ပါဘူး။

satisfies (TypeScript 4.9 ကစပြီး ပါတဲ့) operator က value ကို type တစ်ခုနဲ့ ကိုက်လား စစ်ပေးပေမယ့် value ရဲ့ infer လုပ်ထားတဲ့ type ကို မပြောင်းပါဘူး။ ကိုက်မှ မကိုက်ရင် (ဥပမာ secure: "yes") ချက်ချင်း error ပြပြီး ကိုက်ရင် key တွေနဲ့ value type တွေ အားလုံး ကျန်နေပါတယ်။ as const satisfies Type လို့ တွဲရေးလိုက်ရင် literal တွေကိုပါ ထိန်းထားပါတယ်။ ဒါကို as Type (assertion) နဲ့ မရောပါနဲ့ — as က compiler ကို "ငါ့ကို ယုံ" လို့ ပြောတာဖြစ်လို့ property ကျန်နေတာကိုတောင် မဖမ်းပါဘူး။

ရေးနည်းShape ကို စစ်လားရလာတဲ့ type
const x: T = valueစစ်တယ်T (တိကျမှု ပျောက်)
const x = value as Tအပြည့် မစစ်T
const x = value satisfies Tစစ်တယ်value ရဲ့ ကိုယ်ပိုင် type
မှတ်ထားရမယ့် ပုံစံ
  1. Literal က value တစ်ခုတည်း type
  2. ပြောင်းနိုင်ရင် string အဖြစ် ချဲ့
  3. as const က ချဲ့တာကို ပိတ်
  4. satisfies က စစ်ပြီး type ကို ထိန်း

Annotation က type ကို ဖိချေပြီး satisfies ကတော့ စစ်ရုံပဲ စစ်ပါတယ်။

အထွေထွေ ပုံစံ

typescript
type Choice = "first" | "second";             // literal union

const fixed = { key: "value" } as const;        // readonly, literal types

const config = {
  name: { field: "value" },
} satisfies Record<string, Shape>;             // checked, type kept

const VALUES = ["a", "b"] as const;
type Value = (typeof VALUES)[number];          // "a" | "b"

Literal union က ရွေးချယ်စရာ စာရင်း ဖြစ်ပြီး as const က literal တွေကို ထိန်းပါတယ်။ satisfies က value ကို shape နဲ့ စစ်ပြီး (typeof VALUES)[number] က const array ထဲက element အားလုံးကို union type အဖြစ် ထုတ်ပေးပါတယ်။

Example: စစ်ပြီးသား၊ တိကျတဲ့ route config

ဒီ example က ပြဿနာထဲက route config ကို as const satisfies နဲ့ ပြန်ရေးထားပါတယ်။ Shape ကို Route နဲ့ စစ်တာ မပျောက်သလို key နာမည်တွေနဲ့ path literal တွေကိုလည်း ထိန်းထားပါတယ်။ Type check သက်သက်က ဘာမှ print မထုတ်လို့ ဘာတွေ ထိန်းထားလဲ မြင်ရအောင် console.log line တွေနဲ့ ရလဒ်ကို print ထုတ်ထားပါတယ်။

routes.ts
type Route = { path: string; secure: boolean };

const routes = {
  home: { path: "/", secure: false },
  account: { path: "/account", secure: true },
} as const satisfies Record<string, Route>;

type RouteName = keyof typeof routes; // "home" | "account"

function linkTo(name: RouteName): string {
  const route = routes[name];
  return route.secure ? `${route.path} (login required)` : route.path;
}

const accountPath = routes.account.path; // type: "/account"
console.log(accountPath);
console.log(linkTo("home"));
console.log(linkTo("account"));

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 6} as const satisfies Record<string, Route>;

    as const က literal တွေကို ထိန်းပြီး satisfies က route တိုင်းမှာ path: string နဲ့ secure: boolean ရှိလား စစ်ပါတယ်။ secure ကို ကျန်ခဲ့ရင် ဒီ object မှာပဲ error ပြပါတယ်။

  2. Line 8type RouteName = keyof typeof routes;

    Annotation မသုံးလို့ key နာမည်တွေ ကျန်နေပြီး RouteName က "home" | "account" ဖြစ်ပါတယ်။ Record<string, Route> annotation ဆိုရင် ဒီနေရာမှာ string ပဲ ရမှာပါ။

  3. Line 10function linkTo(name: RouteName): string {

    linkTo("acount") လို စာလုံးပေါင်းမှားရင် အခု compile error ဖြစ်ပါပြီ — ပြဿနာထဲက bug ကို ဒီလို ဖမ်းပါတယ်။

  4. Line 15const accountPath = routes.account.path;

    accountPath ရဲ့ type က string မဟုတ်ဘဲ "/account" ဆိုတဲ့ literal type ပါ၊ as const ကြောင့်ပါ။

You should see
/account
/
/account (login required)

Widening ကို လက်တွေ့ ကြည့်ကြရအောင်

let နဲ့ ကြေညာထားတဲ့ value နဲ့ ရိုးရိုး object property တွေက string အဖြစ် ချဲ့ခံရလို့ Method parameter ဆီ ပို့လို့ မရပါဘူး (comment ပိတ်ထားတဲ့ line တွေကို ဖွင့်ကြည့်ပါ)။ const နဲ့ as const ကတော့ literal ကို ထိန်းထားပါတယ်။

widening.ts
type Method = "GET" | "POST";

function send(method: Method, url: string): string {
  return `${method} ${url}`;
}

let loose = "GET";                  // string (let can change later)
const exact = "GET";                // "GET"
const request = { method: "GET" };  // { method: string }
const frozen = { method: "GET" } as const; // { readonly method: "GET" }

// send(loose, "/a");          // Error: string is not assignable to Method
// send(request.method, "/b"); // Error: same reason
console.log(send(exact, "/users"));
console.log(send(frozen.method, "/orders"));
console.log(loose.length);

satisfies ကို ဘယ်အချိန် သုံးမလဲ

Config object, lookup table နဲ့ theme လို "shape ကို စစ်ချင်တယ်၊ ဒါပေမယ့် key တွေနဲ့ value တွေကို တိတိကျကျ ဆက်သုံးချင်တယ်" ဆိုတဲ့ နေရာတိုင်းမှာ annotation အစား satisfies ကို စဉ်းစားပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Literal union တွေက enum လို ရွေးချယ်စရာ ကန့်သတ်ထားတဲ့ နေရာတိုင်းမှာ သုံးပါတယ် — HTTP method, order status ("pending" | "paid" | "shipped"), button variant ("primary" | "secondary") နဲ့ Tailwind/design-system ရဲ့ size တွေပါ။ React component props မှာ size: "sm" | "md" | "lg" လို ရေးထားရင် editor က ခွင့်ပြုထားတဲ့ value တွေကို autocomplete ပြပေးပါတယ်။

as const satisfies ကတော့ route table, feature flag, i18n message key, permission map နဲ့ theme color token လို config object တွေအတွက် အသုံးများပါတယ်။ ROLES = ["admin", "editor", "viewer"] as const လို array တစ်ခုကနေ type Role ကို ထုတ်ယူထားရင် runtime မှာ dropdown ပြဖို့ array ကို သုံးပြီး compile time မှာ type အဖြစ် သုံးလို့ရလို့ စာရင်း နှစ်ခု ခွဲထိန်းစရာ မလိုတော့ပါဘူး — role အသစ် ထည့်တာ တစ်နေရာတည်းမှာပဲ ဖြစ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. satisfies အစား as Type ကို သုံးတာ

const home = { path: "/" } as Route; က secure ကျန်နေပေမယ့် compile ဖြစ်ပါတယ်၊ assertion က type နှစ်ခု "ဆင်တူရုံ" နဲ့ လက်ခံလို့ပါ။ Value ကို စစ်ချင်တာဆိုရင် satisfies Route ဒါမှမဟုတ် : Route annotation ကို သုံးပါ၊ as ကို တကယ် compiler ထက် ပိုသိတဲ့အခါမှပဲ သုံးပါ။

2. as const က object ကို runtime မှာ freeze လုပ်တယ်လို့ ထင်တာ

as const က compiler ကိုပဲ readonly လို့ ပြောတာဖြစ်ပြီး ထွက်လာတဲ့ JavaScript မှာ ဘာမှ မကျန်ပါဘူး။ any ကနေတစ်ဆင့် ဒါမှမဟုတ် JavaScript code ကနေ property ကို ပြင်လို့ ရနေပါသေးတယ်။ Runtime ကာကွယ်မှု လိုရင် Object.freeze() ကို ထပ်သုံးပါ။

3. let နဲ့ ကြေညာပြီး literal type ရမယ်လို့ မျှော်လင့်တာ

let method = "GET"; ပြီးမှ send(method, url) ခေါ်ရင် "Argument of type 'string' is not assignable to parameter of type 'Method'" ရပါတယ်။ ပြန်မပြောင်းရင် const ကို သုံးပါ၊ ပြောင်းဖို့ လိုရင် let method: Method = "GET"; လို့ type ကို ကိုယ်တိုင် ပေးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ရွေးချယ်စရာ စာရင်းကို as const array တစ်ခုထဲမှာ တစ်ကြိမ်ပဲ ရေးပြီး type ကို အဲဒီကနေ ထုတ်ယူပါ၊ စာရင်း နှစ်ခု မထိန်းပါနဲ့။
  • Config object တွေမှာ annotation အစား satisfies ကို ဦးစားပေးပါ၊ key တွေနဲ့ literal တွေကို ဆက်သုံးနိုင်ပါတယ်။
  • Code review မှာ as Type ကို တွေ့တိုင်း "ဒါ satisfies ဒါမှမဟုတ် annotation ဖြစ်သင့်လား" လို့ မေးပါ။

Mini challenge

Mini challenge: log level config

Log level တွေကို as const array တစ်ခုထဲ တစ်နေရာတည်းမှာ သတ်မှတ်ပြီး အဲဒီကနေ type ထုတ်ကာ satisfies နဲ့ စစ်ထားတဲ့ style config တစ်ခု ဆောက်ပါ။

  • LEVELS = ["debug", "info", "warn", "error"] as const ကနေ type Level ကို ထုတ်ယူရမယ်
  • styles object က level တိုင်းအတွက် { label, enabled } ပါပြီး satisfies Record<Level, LevelStyle> နဲ့ စစ်ရမယ်
  • shouldLog(level, minimum) က enabled ဖြစ်ပြီး minimum ထက် မနိမ့်မှ true ပြန်ရမယ်
  • Level တိုင်းကို "info" minimum နဲ့ စမ်းပြီး print ထုတ်ပါ
log-levels.ts
const LEVELS = /* TODO: the four levels, kept as literals */;
type Level = /* TODO: derive the union from LEVELS */;

type LevelStyle = { label: string; enabled: boolean };

const styles = {
  // TODO: one entry per level, e.g. debug: { label: "DBG", enabled: false }
} /* TODO: check it against Record<Level, LevelStyle> */;

function shouldLog(level: Level, minimum: Level): boolean {
  // TODO: enabled, and position in LEVELS >= position of minimum
}

// TODO: loop over LEVELS and print "<label> logged" or "<label> skipped"

ရလဒ်: DBG skipped INF logged WRN logged ERR logged

အရိပ်အမြွက် ကြည့်ရန်

(typeof LEVELS)[number] က array ထဲက element type အားလုံးကို union အဖြစ် ပေးပါတယ်။ Level တစ်ခု ကျန်ခဲ့ရင် satisfies က "Property 'warn' is missing" လို error ပြပါမယ်။

အဖြေ ကြည့်ရန်
log-levels.ts
const LEVELS = ["debug", "info", "warn", "error"] as const;
type Level = (typeof LEVELS)[number]; // "debug" | "info" | "warn" | "error"

type LevelStyle = { label: string; enabled: boolean };

const styles = {
  debug: { label: "DBG", enabled: false },
  info: { label: "INF", enabled: true },
  warn: { label: "WRN", enabled: true },
  error: { label: "ERR", enabled: true },
} satisfies Record<Level, LevelStyle>;

function shouldLog(level: Level, minimum: Level): boolean {
  return styles[level].enabled && LEVELS.indexOf(level) >= LEVELS.indexOf(minimum);
}

for (const level of LEVELS) {
  const verdict = shouldLog(level, "info") ? "logged" : "skipped";
  console.log(`${styles[level].label} ${verdict}`);
}

Quick Quiz 1 / 3

const request = { method: "GET" }; ဆိုရင် request.method ရဲ့ type က ဘာလဲ။

Quick Quiz 2 / 3

const routes: Record<string, Route> = { home: ... }; နဲ့ const routes = { home: ... } satisfies Record<string, Route>; မှာ keyof typeof routes က ဘာစီ ဖြစ်မလဲ။

Quick Quiz 3 / 3

const home = { path: "/" } as Route; (Route မှာ secure: boolean လည်း လိုအပ်) မှာ ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Literal type က value တစ်ခုတည်းကို ခွင့်ပြုပြီး "GET" | "POST" လို union တွေက ရွေးချယ်စရာ စာရင်း ဖြစ်ပါတယ်။
  • let နဲ့ object property တွေက string အဖြစ် ချဲ့ (widen) ခံရပြီး as const က literal နဲ့ readonly ကို အဆင့်တိုင်းမှာ ထိန်းပါတယ်။
  • satisfies က shape ကို စစ်ပြီး value ရဲ့ တိကျတဲ့ type ကို ထိန်းထားပါတယ်၊ annotation ကတော့ type ကို အစားထိုးပါတယ်။
  • as Type က စစ်ဆေးမှု မဟုတ်ပါ၊ (typeof ARR)[number] နဲ့ const array တစ်ခုကနေ union type ကို ထုတ်ယူနိုင်ပါတယ်။

TypeScript Handbook — TypeScript

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Literal Types, as const & satisfies | Thuta Learning