I recently encountered a challenge where I needed to iterate through unknown properties of an object created from a class. Fortunately, I came across a helpful resource on a webpage that provided some insight.
Initially, my code worked perfectly without any additional methods:
export class TranslateEntry {
datum: string = 'Datum';
sport: string = 'Sport';
leistung: string = 'Leistung';
einheit: string = 'Einheit';
constructor()
{
type ObjectKey = keyof TranslateEntry;
for( let key in this )
{
this[ key as ObjectKey ] = "T" + this[ key as ObjectKey ] + "T";
}
}
}
However, when I added a method to the class, the code stopped functioning:
export class TranslateEntry {
datum: string = 'Datum';
sport: string = 'Sport';
leistung: string = 'Leistung';
einheit: string = 'Einheit';
constructor()
{
type ObjectKey = keyof TranslateEntry;
for( let key in this )
{
this[ key as ObjectKey ] = "T" + this[ key as ObjectKey ] + "T";
}
}
test()
{
}
}
Type 'string' is not assignable to type 'string & (() => void)'.
Type 'string' is not assignable to type '() => void'
Although I understand the concept of union of literal types, explained in this insightful post: What does "keyof typeof" mean in TypeScript?, I have been struggling to resolve this issue.
Any guidance or suggestions would be greatly appreciated.