String key access in typescript

PHOTO EMBED

Fri Aug 19 2022 14:07:07 GMT+0000 (Coordinated Universal Time)

Saved by @Marou03 #typescript

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;
content_copyCOPY

https://bobbyhadz.com/blog/typescript-element-implicitly-has-any-type-expression