keyof का Operator
In this page:
type Keys = keyof ObjectType;
function get<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
Basic keyof
एक object type के लिए, keyof इसके known property names को string literal types के रूप में containing एक union produce करता है, code को generically 'इस type की कोई भी valid key' के साथ काम करने देते हुए बजाय एक fixed, hardcoded key के।
उदाहरण: Basic keyof
interface User {
id: number;
name: string;
}
type UserKeys = keyof User; // "id" | "name"
let key: UserKeys = "name";
console.log(key);
Generic Functions के साथ keyof
K extends keyof T pattern ensure करता है कि एक supplied key genuinely object type T पर मौजूद है, runtime पर silently undefined return करने के बजाय compile time पर property name में एक typo पकड़ते हुए।
उदाहरण: keyof with Generic Functions
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
const user = { id: 1, name: "Wei" };
console.log(getProperty(user, "name"));
keyof और Return Types
Indexed access type T[K] K द्वारा selected property की type represent करता है, इसलिए एक function का return type automatically match कर सकता है जो property actually request की गई थी, any तक widen होने के बजाय।
उदाहरण: keyof and Return Types
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user = { id: 1, name: "Wei" };
const name: string = getProperty(user, "name");
console.log(name);
Properties Update करने के लिए keyof
वही K extends keyof T pattern एक selected property key और इसकी matching value दोनों को साथ type कर सकता है, जो exactly वही है जो एक type-safe generic getter या setter function को चाहिए।
उदाहरण: keyof for Updating Properties
function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): void {
obj[key] = value;
}
const user = { id: 1, name: "Wei" };
setProperty(user, "name", "Lin");
console.log(user);
Interfaces के साथ keyof
Interfaces को keyof और generics के साथ combine किया जा सकता है reusable property-reading utilities बनाने के लिए जो किसी भी interface shape पर पूरी तरह type-checked रहती हैं जिस पर वे apply होती हैं।
उदाहरण: keyof with Interfaces
interface Product {
id: number;
price: number;
}
function getValue<K extends keyof Product>(product: Product, key: K) {
return product[key];
}
console.log(getValue({ id: 1, price: 20 }, "price"));
Mapped Types के साथ Keyof
एक mapped type keyof T के ऊपर { [K in keyof T]: ... } syntax उपयोग करके iterate कर सकता है, जो T के हर property name को एक बार में visit करता है, एक नई type generate होने देते हुए जो T की keys को mirror करती है जबकि हर property की value type को transform करती है।
उदाहरण: Keyof with Mapped Types
interface User {
id: number;
name: string;
}
type ReadonlyUser = { readonly [K in keyof User]: User[K] };
const user: ReadonlyUser = { id: 1, name: "Sana" };
console.log(user);
typeof Operator
एक type position में, typeof एक existing variable या constant लेता है और वह type produce करता है जो TypeScript ने पहले से infer की है, type को हाथ से फिर से declare करने से बचते हुए और जब भी value का shape बदले type को automatically sync में रखते हुए।
उदाहरण: The typeof Operator
const config = { retries: 3, timeout: 1000 };
type ConfigType = typeof config; // { retries: number; timeout: number }
let another: ConfigType = { retries: 5, timeout: 2000 };
console.log(another);
Keyof Typeof Pattern
keyof को typeof के साथ combine करना, जैसे keyof typeof someObject में, एक runtime object के खुद के property names का string literal types के रूप में एक union produce करता है, जो directly एक plain JavaScript object से एक type-safe key set derive करने का एक common तरीका है।
उदाहरण: Keyof Typeof Pattern
const colors = { red: "#f00", green: "#0f0" };
type ColorKey = keyof typeof colors; // "red" | "green"
let key: ColorKey = "red";
console.log(colors[key]);
Records पर Generic Constraints के साथ Keyof
keyof operator Record<K, V> और multiple type parameters के साथ भी अच्छी तरह काम करता है, एक function को एक generic parameter को दूसरे की एक key होने तक constrain करने देते हुए, जो उन utilities के लिए उपयोगी है जो किसी भी object की keys और values पर साथ generically operate करती हैं।
उदाहरण: Keyof with Generic Constraints on Records
function getFromRecord<K extends keyof R, R extends Record<string, number>>(record: R, key: K) {
return record[key];
}
const scores = { math: 90, science: 85 };
console.log(getFromRecord(scores, "math"));
- यह उम्मीद करना कि
keyof Uservalues देगा, जब यह property names को एक union के रूप में देता है जैसे"id" | "name"। key: stringउपयोग करना बजायK extends keyof Tके, जो typos allow करता है औरobj[key]को एक compile error बनाता है।keyof typeof objकोkeyof Typeके साथ confuse करना, क्योंकि एक value परkeyofको पहलेtypeofचाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: