I am trying to achieve valid DTO generation using typescript-ref utility from ServiceStack. Problem is: for nullable and reference properties it will not generate the default value definition.
There is C# definition of a DTO:
public class Data
{
public int Value { get; set; }
public int? OptionalValue { get; set; }
public string Text { get; set; }
}
Generated typescript DTO will look like this:
export class Data
{
public value: number;
public optionalValue: number;
public text: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
This will lead to static checking problems. You will not be able to set undefined or null value (whatever will be selected to represent C# null value) to those properties. Because of Partial constructor it is possible to just omit them, but still it isn't convenient.
Also TypeScript compiler will not know that those fields can have undefined value - this is where we will lose static checking for those DTOs completely.
I found MakePropertiesOptional: True documented option that will make every property in generated DTOs optional. But this not solving my problem but leads to more of them. Is there any more flexible ways to solve it?
I need to have generated DTO for above class to look like this:
export class Data
{
public value: number;
public optionalValue?: number;
public text?: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}