const str = 'name' as string; const obj = { name: 'James Doe', country: 'Chile', }; // 👇️ "James Doe" console.log(obj[str as keyof typeof obj]); // 👇️ type T = "name" | "country" type T = keyof typeof obj;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter