AdaskoTheBeAsT / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
62 stars 12 forks source link

Why was there a breaking change for nullable types? #33

Closed RudeySH closed 11 months ago

RudeySH commented 1 year ago

In Typewriter v2.12.0 (commit 9113fa1), a breaking change was introduced for nullable types. Type.Name is appended with " | null" by default now. I don't think this is good default behavior, unless it can be disabled somehow?

Consider this. There are at least 5 different ways of defining nullable, undefinable or optional properties in TypeScript:

interface Example {
    prop1: string | null;
    prop2: string | undefined;
    prop3: string | null | undefined;
    prop4?: string;
    prop5?: string | null;
}

In my projects, I almost always use the fourth option. In C#, I have configured my JSON serializers to prevent null-valued properties from being serialized, so my API never returns the null value.

Now I'm forced to add this to my template everywhere I use Type.Name:

item.Type.Name.EndsWith(" | null") ? item.Type.Name.Substring(0, item.Type.Name.Length - 7) : item.Type.Name

Please consider reverting this breaking change, or at the very least make it configurable. It has already caused other issues:

AdaskoTheBeAsT commented 1 year ago

I will make it optional on some weekend breaking change was introduced due to https://github.com/Microsoft/TypeScript/pull/7140

as I would like to have strict null checks in frontend in same way as it is in c# with nullable enable

AdaskoTheBeAsT commented 11 months ago

@RudeySH I added possibility to disable | null on nullable types by using DisableStrictNullGeneration in settings

Template(Settings settings)
{
    settings
        .DisableStrictNullGeneration()
        ;
}

released in https://github.com/AdaskoTheBeAsT/Typewriter/releases/tag/v2.14.0