TypeScript Pobierz właściwość obiektu po nazwie

// Type
type UserType = {
	name: string;
    email: string;
};

// Object
const user: UserType = {
	name: "Bob",
    email: "[email protected]"
};

// Get Object property by name
const name = user["name" as keyof UserType] // name = "Bob"

// Alternatively, create object property type
type ObjectKey = keyof typeof user; // type ObjectKey = "name" | "email"

const myKey = "name" as ObjectKey;
const name = user[myKey]; // name = "Bob"
Tomanator