I have defined a union type for my Data Transfer Object (DTO) that includes various property types such as text, date, and number. Now, I am trying to create a class based on this DTO, but I am encountering an issue where the type of the 'value' property is showing up as unknown instead of the expected type.
export enum PropertyType {
Text = 'text',
Date = 'date',
Number = 'number',
//...
}
export interface PropertyDtoBase {
prompt?: string;
required?: boolean;
// ...
};
export type PropertyDto = PropertyDtoBase & (
{ type: PropertyType.Text; value?: string; } |
{ type: PropertyType.Date; value?: Date; } |
{ type: PropertyType.Number; value?: number; } |
// ...
);
In the generated class, I am attempting to carry over the type definition for the 'value' property from the DTO. However, it is only displaying as unknown. How can I maintain the correct type for the 'value' property in the class?
export class Property {
prompt?: string;
required?: boolean;
type?: PropertyType;
value?: Exclude<ConstructorParameters<typeof Property>[0], undefined>["value"];
// ...
public constructor(dto?: PropertyDto) {
Object.assign(this, dto);
//...
}
};
const x = new Property({ type: PropertyType.Text, value: "test" });
if(x.type === PropertyType.Text) {
x.value // value is unknown here, but should be string
}
If anyone has suggestions on how to preserve the correct typing for the 'value' property from the DTO within the generated class, please let me know!