Nullable type generation

The docs here state: https://docs.servicestack.net/typescript-add-servicestack-reference#typescript-nullable-properties

That if in c# I have a property like:

public string? Name { get; set; }

Then it will by default output as option in typescript like:

public name?: string;

But it actually outputs like:

public name: string;

Why am I getting different behaviour to docs?

edit:

It seems like if I set MakePropertiesOptional: True then everything becomes nullable but then if I add [ValidateNotNull] if will output that property as not nullable.

This seems to be different to what docs say is default behaviour.

How do I specify what fields I want nullable/non-nullable on return types?

The docs don’t show an example of nullable reference types but I’ve fine tuned the behavior to generated better expected results for them. Where in the latest v6.4.1+ on MyGet this C# DTO:

public class Data
{
    public int Value { get; set; }
    public int? OptionalValue { get; set; }
    public string Text { get; set; }
    public string? OptionalText { get; set; }

    [Required]
    public int RequiredValue { get; set; }
    [Required]
    public string RequiredText { get; set; }

    // Override default behavior
    [ApiMember(IsRequired=true)]
    public string? OverrideRequiredText { get; set; }

    [ApiMember(IsOptional=true)]
    public string OverrideOptionalText { get; set; }
}

Will generate the TypeScript DTO:

export class Data
{
    public value: number;
    public optionalValue?: number;
    public text: string;
    public optionalText?: string;
    // @Required()
    public requiredValue: number;

    // @Required()
    public requiredText: string;

    // @ApiMember(IsRequired=true)
    public overrideRequiredText: string;

    // @ApiMember(IsOptional=true)
    public overrideOptionalText?: string;
    //...
}

Which includes a new [ApiMember(IsOptional=true)] to override default behavior and force an optional type.


FYI we expect next ServiceStack v6.5 to be released this week if you prefer to wait for the official release on NuGet.

1 Like

Thanks Mythz that’s great!

1 Like