type TKeyed = { [key: string]: any };
class MyClass<T extends TKeyed> {
private keysMap: Map<string, any>;
constructor() {
const keys = Object.keys({} as T); // Retrieve the keys of T
this.keysMap = new Map<string, any>();
for (const key of keys) {
this.keysMap.set(key, null); // Store the keys in the Map
}
}
getKeysMap(): Map<string, any> {
return this.keysMap;
}
}
interface MyType extends TKeyed {
prop1: string;
prop2: number;
}
const myInstance = new MyClass<MyType>();
const keysMap = myInstance.getKeysMap();
console.log(keysMap); // Output: Map { 'prop1' => null, 'prop2' => null }
This is what I'm trying to output:
// Output: Map { 'prop1' => null, 'prop2' => null }
But, the result is:
Map (0) {}
I've tried the above code using those methods to extract the keys:
const keys = Reflect.ownKeys({} as T);
It didn't work either.