I have a const object which looks like this:
export const Language: Values = {
es: {urlValue: 'es', label: 'Spain'},
en: {urlValue: 'en', label: 'Anything'},
eb: {urlValue: 'eb', label: 'Other thing'},
};
export interface Values {
[name: string]: Value;
}
export interface Value {
urlValue;
label;
selected?;
}
Now somewhere in app, we read some data which has this format, which coincidentally has the exact same keys:
{
es: 32,
en: 11,
eb: 56
}
So actually i need another object type like this:
export class AnotherObject {
constructor(public es: number,
public en: number,
public eb: number) {
}
}
But can i create this type also dynamically so that it automatically uses the keys of the Language object, or urlValue of "Value" type?
PS: The question is a simplified form of our exact use case, where we have multiple consts like Languages. So an automatism would be really helpful.
Edit: For the suggestion of @basarat - Since this object is a part of a parent object i actually need a type definiton in this part:
export class ParentObject {
thatObjectAbove: AnotherObject;
otherObjects: ..
..
}
export interface AnotherObject {
[name: LanguageOptions]: number;
}
I have the error "An index signature parameter type cannot be a union type. Consider using a mapped object type instead." on the 'name' part of line "[name: LanguageOptions]: number;"
