keyof နှင့် Utility Types

7.0.2

Field နာမည်တွေ တူညီနေဖို့ မူလ type ကနေ ဆက်စပ် types တွေ ဆောက်ပါ။

ဒီစာမျက်နှာမှာ

တကယ်ရှိတဲ့ Field နာမည်တွေပဲ လက်ခံခြင်း

keyof က type တစ်ခုရဲ့ keys တွေကို union အဖြစ် ထုတ်ပေးပါတယ်။ Generic constraint နဲ့ object မှာ တကယ်ရှိတဲ့ key ကိုပဲ ပေးနိုင်ပါတယ်။ T[K] က အဲဒီ property ရဲ့ type ပြန်ပါတယ်။

function readField<T, K extends keyof T>(row: T, key: K): T[K] {
  return row[key];
}

const price = readField({ name: "Pen", price: 2 }, "price");

View သို့ Patch အတွက် Type ဆောက်ခြင်း

Pick က fields ရွေးပြီး Omit က fields ဖယ်ပါတယ်။ Partial က top level fields ကို optional လုပ်ပါတယ်။ အဲဒီ types တွေက compiler အတွက် သတ်မှတ်ချက်ပါ။ Runtime object ထဲက fields ကို မဖယ်ပေးပါဘူး။

type Product = { id: number; name: string; price: number };
type ProductCard = Pick<Product, "name" | "price">;
type ProductUpdate = Partial<Omit<Product, "id">>;

တရားဝင် ကိုးကားချက်များ