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

Tuples

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

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

  • Array type နဲ့ tuple type ရဲ့ ကွာခြားချက်ကို ရှင်းပြနိုင်ဖို့
  • Function တစ်ခုကနေ value နှစ်ခု ပြန်ပေးဖို့ tuple ကို return type အဖြစ် သုံးပြီး destructure လုပ်နိုင်ဖို့
  • Labeled, optional (?) နဲ့ rest (...) tuple element တွေ ရေးနိုင်ဖို့
  • Mutable tuple ရဲ့ push() ကွက်လပ်ကို readonly နဲ့ ပိတ်နိုင်ဖို့

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

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

မြေပုံပေါ်က နေရာတစ်ခုကို latitude, longitude ဂဏန်း နှစ်လုံးနဲ့ ဖော်ပြတာကို စဉ်းစားကြည့်ပါ။ ဂဏန်း နှစ်လုံး အမြဲ ပါရပြီး အစဉ်ကလည်း အရေးကြီးပါတယ် — ပြောင်းပြန် ဖြစ်သွားရင် တခြား နိုင်ငံကို ရောက်သွားပါမယ်။ Tuple က ဒီလို "အရေအတွက် သတ်မှတ်ထား၊ position တစ်ခုချင်းမှာ အဓိပ္ပာယ်ရှိ" တဲ့ list တိုလေးတွေအတွက်ပါ။

[number, string] ဆိုတဲ့ tuple type က "element နှစ်ခု၊ ပထမက number၊ ဒုတိယက string" လို့ ပြောပါတယ်။ ရိုးရိုး array ကတော့ element type တစ်မျိုးကိုပဲ သိပြီး ဘယ်နှခု ရှိမလဲ မသိပါဘူး။

Problem → Why → Solution

ပြဿနာ: return လုပ်တဲ့ array က position ကို မေ့သွားတယ်

"Mya, 42" လို CSV line တစ်ကြောင်းကနေ နာမည်နဲ့ ရမှတ်ကို ခွဲထုတ်တဲ့ function တစ်ခု ရေးတယ် ဆိုပါစို့။ Value နှစ်ခု ပြန်ပေးဖို့ array တစ်ခု return လုပ်လိုက်ပါတယ်။ Return type မရေးထားတော့ TypeScript က (string | number)[] လို့ infer လုပ်ပြီး ဘယ် position မှာ ဘာရှိလဲဆိုတဲ့ အချက်အလက် ပျောက်သွားပါတယ်။ ရလဒ်က points.toFixed() ကို ခေါ်လို့ မရတော့တာပါ — points က string ဖြစ်နိုင်တယ်လို့ compiler က ထင်နေလို့ပါ။

scores.ts
function parseEntry(line: string) {
  const [name, raw] = line.split(",");
  return [name.trim(), Number(raw)]; // inferred: (string | number)[]
}

const [player, points] = parseEntry("Mya, 42");
points.toFixed(0);
// Error: Property 'toFixed' does not exist on type 'string | number'.

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

Array literal တစ်ခုကို TypeScript က default အနေနဲ့ array type အဖြစ် infer လုပ်ပါတယ်၊ နောက်ပိုင်း push လုပ်ချင်တာ ဖြစ်နိုင်လို့ပါ။ Position အလိုက် type ကို ထိန်းထားချင်ရင် function ရဲ့ return type ကို [string, number] လို့ ကိုယ်တိုင် ရေးရပါမယ်။ ဒါဆို player က string၊ points က number ဖြစ်သွားပြီး value နှစ်ခုကို ပြောင်းပြန် return လုပ်မိရင်လည်း compile error တက်ပါတယ်။

Core concept: position တစ်ခုချင်းမှာ type

Tuple type ကို [Type1, Type2, ...] လို့ ရေးပါတယ်။ ဒီ type က အချက် နှစ်ခုကို သိပါတယ် — element ဘယ်နှခု ရှိလဲ (length) နဲ့ position တစ်ခုချင်းမှာ ဘာ type ရှိလဲပါ။ ဒါကြောင့် let user: [number, string] ဆိုရင် user[0] က number၊ user[1] က string ဖြစ်ပြီး user[2] ကို ဖတ်ရင် "Tuple type '[number, string]' of length '2' has no element at index '2'" error ရပါတယ်။ ["Steve", 1] လို အစဉ်ပြောင်းပြန် ထည့်ရင်လည်း error ပါ။

Tuple နဲ့ အတွဲညီဆုံးက destructuring ပါ။ const [id, name] = user; လို့ ရေးလိုက်ရင် id က number၊ name က string ဖြစ်ပြီး variable နာမည်ကို ကိုယ်ကြိုက်သလို ပေးလို့ ရပါတယ်။ React ရဲ့ useState() က ဒီ pattern ကို သုံးတာပါ — const [count, setCount] = useState(0); ထဲက value နဲ့ setter function က tuple တစ်ခုအဖြစ် ပြန်လာတာပါ။

သတိထားရမှာက TypeScript က array literal ကနေ tuple ကို အလိုအလျောက် infer မလုပ်ပေးတာပါ။ const pair = [1, "a"]; က (string | number)[] ဖြစ်ပါတယ်။ Tuple ရဖို့ နည်း သုံးမျိုး ရှိပါတယ် — variable ကို annotate လုပ်တာ၊ function ရဲ့ return type ရေးတာ ဒါမှမဟုတ် as const ထည့်တာ (ဒါက readonly [1, "a"] လို literal type တွေပါ ရပါတယ်)။

Runtime မှာတော့ tuple ဆိုတာ ရိုးရိုး JavaScript array တစ်ခုပါပဲ၊ length ကို ဘယ်သူမှ မစောင့်ကြည့်ပါဘူး။ ဒါကြောင့် mutable tuple ပေါ်မှာ push() ကို compiler က ခွင့်ပြုနေတုန်းပါ — element type တစ်ခုခုနဲ့ ကိုက်ရင် လက်ခံလိုက်ပါတယ်။ ဒီကွက်လပ်ကို ပိတ်ဖို့ readonly [number, string] လို readonly tuple ကို သုံးပါ။

ဒါဆို tuple နဲ့ object ဘယ်ဟာ ရွေးမလဲ။ အဓိက မေးခွန်းက "value တွေကို position နဲ့ ခွဲသိလား၊ နာမည်နဲ့ ခွဲသိလား" ပါ။ [x, y] လို သဘာဝကျတဲ့ အစဉ်ရှိပြီး value နှစ်ခု သုံးခုပဲ ရှိရင် tuple က တိုတိုရှင်းရှင်း ဖြစ်ပါတယ်။ Value တွေ များလာတာ ဒါမှမဟုတ် အစဉ်က အဓိပ္ပာယ် မရှိတာ ဆိုရင် { id, name, email } လို object က ပိုလုံခြုံပါတယ်၊ property နာမည်က ဘာလဲဆိုတာ ကိုယ်တိုင် ပြောပြနေလို့ပါ။

number[][number, string]
Lengthဘယ်လောက်မဆိုအမြဲ 2
Element typeအားလုံး တူposition အလိုက် ကွာ
Index 5 ကို ဖတ်ရင်number (undefined ဖြစ်နိုင်)compile error
မှတ်ထားရမယ့် ပုံစံ
  1. အကွက် အရေအတွက် သတ်မှတ်ထား
  2. အကွက်တိုင်းမှာ ကိုယ်ပိုင် type
  3. Destructure လုပ်ရင် type ပါလာ
  4. readonly က အကွက်အသစ် ထပ်မတိုးစေ

Array က "ဒီလို ပစ္စည်းတွေ အများကြီး"၊ tuple က "ဒီအစဉ်အတိုင်း ဒီပစ္စည်း ဒီလောက်" လို့ ပြောပါတယ်။

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

typescript
let pair: [TypeA, TypeB] = [a, b];
const [first, second] = pair;            // first: TypeA, second: TypeB

type Labeled = [label: TypeA, other: TypeB];
type WithOptional = [TypeA, TypeB?];
type WithRest = [TypeA, ...TypeB[]];
type Fixed = readonly [TypeA, TypeB];

function name(): [TypeA, TypeB] { return [a, b]; }

Square bracket ထဲမှာ position အလိုက် type တွေကို အစဉ်လိုက် ရေးပါတယ်။ Label တွေက documentation အတွက်ပဲ ဖြစ်ပြီး ? က နောက်ဆုံး element ကို optional လုပ်ကာ ... က အရေအတွက် မကန့်သတ်တဲ့ အဆုံးပိုင်းကို ဖော်ပြပါတယ်။

Example: [number, string] tuple

ဒီ example က tuple variable ကို အရင် ကြေညာပြီးမှ value ထည့်ပါတယ်။ Comment ပိတ်ထားတဲ့ line က အစဉ်ပြောင်းပြန် ထည့်ရင် ဘာဖြစ်မလဲ ပြပါတယ်။ Comment ကို ဖြုတ်ပြီး error ကို ကိုယ်တိုင် ကြည့်ပါ၊ ပြီးရင် user[2] ကို ဖတ်ကြည့်ပြီး length error ကိုလည်း စမ်းပါ။

typescript
// Declare a tuple type
let user: [number, string];

// Initialize it
user = [1, "Steve"];

// user = ["Steve", 1]; // Error: Type 'string' is not assignable to type 'number'.

console.log(user[1]);

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

  1. Line 2let user: [number, string];

    user က element နှစ်ခု ပါတဲ့ tuple ဖြစ်ရပါမယ် — position 0 မှာ number (id)၊ position 1 မှာ string (နာမည်) ပါ။

  2. Line 5user = [1, "Steve"];

    Value နှစ်ခုလုံး မှန်ကန်တဲ့ position မှာ ရှိလို့ လက်ခံပါတယ်။ [1] ဒါမှမဟုတ် [1, "Steve", true] ဆိုရင် length မကိုက်လို့ error ရပါတယ်။

  3. Line 7// user = ["Steve", 1];

    Comment ဖြုတ်လိုက်ရင် "Type 'string' is not assignable to type 'number'" ရပါတယ်။ Position 0 မှာ string ရောက်နေလို့ပါ — ရိုးရိုး (string | number)[] array ဆိုရင် ဒီအမှားကို မဖမ်းနိုင်ပါဘူး။

  4. Line 9console.log(user[1]);

    user[1] ရဲ့ type က string ဖြစ်ကြောင်း compiler သိလို့ user[1].toUpperCase() ကိုလည်း လုံခြုံစွာ ခေါ်နိုင်ပါတယ်။ Output က Steve ပါ။

You should see
Steve

Labeled, optional နဲ့ rest element များ

[name: string, score: number] ထဲက label တွေက editor hover နဲ့ error message တွေမှာ ပေါ်လာပြီး ဖတ်ရလွယ်စေပေမယ့် destructure လုပ်တဲ့ variable နာမည်ကို မကန့်သတ်ပါဘူး။ end?: number လို optional element က tuple ရဲ့ အဆုံးမှာပဲ ရှိနိုင်ပြီး ...details: string[] လို rest element က "ပထမ element ပြီးရင် string ဘယ်နှခုမဆို" လို့ ဆိုလိုပါတယ်။ အောက်က code မှာ parseEntry() ကို Entry ဆိုတဲ့ return type ရေးထားတာ သတိထားကြည့်ပါ — lesson အစက ပြဿနာကို ဒါက ဖြေရှင်းပေးပါတယ်၊ points က number ပြန်ဖြစ်လာလို့ points + 8 က တကယ့် ဂဏန်းပေါင်းခြင်း ဖြစ်ပါတယ်။

tuple-forms.ts
type Entry = [name: string, score: number];

function parseEntry(line: string): Entry {
  const [name, raw] = line.split(",");
  return [name.trim(), Number(raw)];
}

const [player, points] = parseEntry("Mya, 42");
console.log(player.toUpperCase(), points + 8);

type Span = readonly [start: number, end?: number];
const fromFive: Span = [5];
const fiveToNine: Span = [5, 9];
console.log(fromFive.length, fiveToNine.length);

type LogLine = [level: string, ...details: string[]];
const line: LogLine = ["warn", "disk", "85%"];
console.log(line[0], line.slice(1).join(" "));

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

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

Tuple ကို နေ့စဉ် အများဆုံး တွေ့ရတာက React hook တွေမှာပါ — useState() က [value, setValue] ကို၊ custom hook အများစုကလည်း [data, error] ဒါမှမဟုတ် [isOpen, toggle] လို tuple တွေ ပြန်ပေးပါတယ်။ Tuple ဖြစ်လို့ caller က variable နာမည်ကို ကိုယ်ကြိုက်သလို ပေးနိုင်ပါတယ်၊ object ဆိုရင် property နာမည်တွေကို rename လုပ်ရပါမယ်။

တခြား အသုံးများတဲ့ နေရာတွေကတော့ [latitude, longitude] လို coordinate၊ [r, g, b] color value၊ Object.entries() က ပြန်ပေးတဲ့ [key, value] pair တွေနဲ့ CSV row တွေပါ။ Promise.all() ကိုလည်း tuple နဲ့ type ပေးထားပါတယ် — const [user, orders] = await Promise.all([loadUser(), loadOrders()]); မှာ variable တစ်ခုချင်းက ကိုယ့် promise ရဲ့ ရလဒ် type ကို ရပါတယ်။ Field သုံးလေးခုထက် များလာရင်တော့ tuple အစား object ကို ပြောင်းသုံးကြပါတယ် — row[3] ဆိုတာ ဘာလဲ မှတ်မိဖို့ ခက်ပေမယ့် row.isActive ကတော့ ကိုယ်တိုင် ရှင်းပြနေလို့ပါ။

အမှားများ

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

1. Mutable tuple ရဲ့ length ကို compiler က ကာကွယ်တယ်လို့ ထင်တာ

let t: [number, string] = [1, "a"]; t.push(2); က compile ဖြစ်ပြီး t.length က 3 ဖြစ်သွားပါတယ်။ Type ကတော့ element နှစ်ခုလို့ ဆက်ပြောနေပါတယ်။ Tuple ကို မပြောင်းလဲသင့်ရင် readonly [number, string] ပေးပါ၊ ဒါဆို push() ကိုယ်တိုင် မရှိတော့ပါဘူး။

2. Return type မရေးဘဲ tuple ရမယ်လို့ မျှော်လင့်တာ

return [name, score]; ကိုပဲ ရေးထားရင် (string | number)[] ရပြီး destructure လုပ်တဲ့ variable တိုင်းက union type ဖြစ်သွားပါတယ်။ Function ရဲ့ return type ကို : [string, number] လို့ ရေးပါ ဒါမှမဟုတ် return value ကို as const လုပ်ပါ။

3. Field များတဲ့ record ကို tuple နဲ့ ဖော်ပြတာ

[string, string, number, boolean, string] လို tuple မှာ position တွေကို မှားရောနိုင်ပြီး string နှစ်ခု ပြောင်းပြန် ဖြစ်သွားရင် compiler ကလည်း မဖမ်းနိုင်ပါဘူး (type တူနေလို့)။ ဒီလို data အတွက် နာမည်ပါတဲ့ property တွေရှိတဲ့ object type ကို သုံးပါ။

Best Practices

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

  • Tuple ကို element နှစ်ခု သုံးခုလောက်ပဲ ရှိပြီး အစဉ်က သဘာဝကျတဲ့ data (coordinate, key-value pair) အတွက် သုံးပါ။
  • Position တွေရဲ့ အဓိပ္ပာယ် ရှင်းအောင် label တွေ ထည့်ပါ: [lat: number, lng: number]။
  • Function ကနေ ပြန်ပေးတဲ့ tuple တွေကို ပြင်စရာ မလိုရင် readonly လုပ်ထားပါ။

Mini challenge

Mini challenge: color နဲ့ min/max

RGB color tuple ကို hex string အဖြစ် ပြောင်းတဲ့ function နဲ့ number list တစ်ခုရဲ့ အငယ်ဆုံး/အကြီးဆုံးကို tuple အဖြစ် ပြန်ပေးတဲ့ function ကို ရေးပါ။

  • RGB type က label ပါတဲ့ readonly tuple [red, green, blue] ဖြစ်ရမယ်
  • toHex([255, 136, 0]) က "#ff8800" ပြန်ရမယ်
  • minMax(values) က [min, max] tuple ပြန်ပြီး list ဗလာဆိုရင် undefined ပြန်ရမယ်
  • minMax ရဲ့ ရလဒ်ကို destructure လုပ်ပြီး print ထုတ်ပါ
tuples-practice.ts
type RGB = /* TODO: labeled readonly tuple of three numbers */;

function toHex(color: RGB): string {
  // TODO: each channel -> 2 hex digits, prefixed with "#"
}

function minMax(values: readonly number[]) /* TODO: return type */ {
  // TODO: undefined for an empty list, otherwise [min, max]
}

// TODO: print toHex([255, 136, 0]) and the min/max of [7, 2, 9, 4] and []

ရလဒ်: အောက်ပါအတိုင်း ထွက်ရပါမယ်: #ff8800 min 2, max 9 no values

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

c.toString(16).padStart(2, "0") က 0-255 ဂဏန်းကို hex digit နှစ်လုံး ဖြစ်အောင် ပြောင်းပေးပါတယ်။ Math.min(...values) နဲ့ Math.max(...values) ကို သုံးနိုင်ပါတယ်။

အဖြေ ကြည့်ရန်
tuples-practice.ts
type RGB = readonly [red: number, green: number, blue: number];

function toHex(color: RGB): string {
  return "#" + color.map((c) => c.toString(16).padStart(2, "0")).join("");
}

function minMax(values: readonly number[]): [min: number, max: number] | undefined {
  if (values.length === 0) return undefined;
  return [Math.min(...values), Math.max(...values)];
}

const brand: RGB = [255, 136, 0];
console.log(toHex(brand));

const result = minMax([7, 2, 9, 4]);
if (result) {
  const [low, high] = result;
  console.log(`min ${low}, max ${high}`);
}
console.log(minMax([]) ?? "no values");

Quick Quiz 1 / 3

Annotation မပါဘဲ const pair = [1, "a"]; လို့ ရေးရင် pair ရဲ့ type က ဘာလဲ။

Quick Quiz 2 / 3

let t: [number, string] = [1, "a"]; t.push(2); console.log(t.length); ကို run ရင် ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

type Span = [number, number?]; ဆိုရင် ဘယ် value က Span မဟုတ်လဲ။

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

  • Tuple [A, B] က length သတ်မှတ်ထားပြီး position တစ်ခုချင်းမှာ ကိုယ်ပိုင် type ရှိတဲ့ array ပါ။
  • Array literal က tuple အဖြစ် အလိုအလျောက် မဖြစ်ပါ — annotation, return type ဒါမှမဟုတ် as const လိုပါတယ်။
  • Label, ? နဲ့ ... တို့က tuple တွေကို ပိုဖတ်လွယ်ပြီး ပိုပြောင်းလွယ်စေပါတယ်။
  • Runtime မှာ tuple က ရိုးရိုး array ဖြစ်လို့ push() ကွက်လပ်ကို readonly နဲ့ ပိတ်ပြီး field များတဲ့ data အတွက် object ကို သုံးပါ။

TypeScript Handbook — TypeScript

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

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

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

Tuples | Thuta Learning