Suppose I have the following definitions:
export enum Category {
car = 'car',
truck = 'truck',
}
export interface IProperty {
propertyName: string,
propertyLabel: string,
propertyType: 'string' | 'number',
}
export interface CategoryDefinition {
category: Category,
label: string,
properties: IProperty[]
}
and then have definitions like that:
export class Definitions {
public static get car(): CategoryDefinition {
return {
category: Category.car,
label: "Car",
properties: [
{
propertyName: 'weight',
propertyLabel: 'Weight',
propertyType: 'number'
},
{
propertyName: 'color',
propertyLabel: 'Color',
propertyType: 'string'
}
],
};
}
public static get truck(): CategoryDefinition {
return {
category: Category.truck,
label: "Truck",
properties: [
{
propertyName: 'payload',
propertyLabel: 'Payload',
propertyType: 'number'
},
{
propertyName: 'axles',
propertyLabel: 'Axles',
propertyType: 'number'
}
],
};
}
}
And now, I would like to generate interfaces from this definition, which should result in this:
export interface Car {
weight: number,
color: string
}
export interface Truck{
payload: number,
axles: number
}
Ideally done someway like:
export interface Car = createInterface('Car', Definitions.car.properties);
export interface Truck = createInterface('Truck', Definitions.truck.properties);
Reason being that I have to deal with about 30-40 of those definitions. I can of course define their interface on their own (API responses), but then I would have to re-define their properties for Ui display, and this would be error-prone. This way I would only have to define their properties and could generate their corresponding interface.
I have found an answer that maybe hint in the right direction, however I feel that it's not quite the same and I could not get it to work.
The Definitions would be fixed (i.e. not dynamically changed/updated), if that helps.
Any help would be appreciated.