Discover how to retrieve the type of properties from an object by following this Typescript tutorial link. However, it seems to be behaving like Javascript and returning the value of the property instead of the type:
const x = { foo: 10, bar: 'hello!' };
const foo = getProperty(x, 'foo'); // number
console.log(foo);
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key]; // Inferred type is T[K]
}
As a result, it displays the value 10 instead of the type number as expected. Any thoughts on this matter?
To gain more clarity, take a look at this link regarding TypeScript keyof and Lookup Types. The intention is for the above snippet in the Angular component to provide the type of the property, not the actual value:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html