vojtechhabarta / typescript-generator

Generates TypeScript from Java - JSON declarations, REST service client
MIT License
1.15k stars 236 forks source link

Nullability in Kotlin #651

Open d00mw01f opened 3 years ago

d00mw01f commented 3 years ago

Hi,

I read old pull request about nullability and I'm trying to achieve similar behavior:

  1. Using Kotlin nullability as optional via OptionalProperties.useLibraryDefinition
  2. Have optional parameters as undefined in Typescript (so I can skip them during object creation)
  3. Enabling JsonInclude.Include.NON_NULL to avoid NULL properties in JSON

For example I wish to use:

class Test(
    val text: String? = null,
    val list: List<String?> = listOf("item", null)
)

With enabled JsonInclude.Include.NON_NULL serialized object will look the same as in the ticket:

{
    "list": [
        "item",
        null
    ]
}

But Typescript annotations with OptionalProperties.useLibraryDefinition would be

export interface Test {
    list: (string | null)[];
    text?: string | null;
}

Is there any way to make optional types get rid of nullability? E.g.:

export interface Test {
    list: (string | null)[];
    text?: string;
}

Thanks.

vojtechhabarta commented 3 years ago

Try setting nullabilityDefinition parameter to undefinedInlineUnion, see also #648.

d00mw01f commented 3 years ago

This setting fixes optional properties but breaks list:

export interface Test {
    list: (string | undefined)[];
    text?: string;
}

The list contains null and string, while the interface says that it can contain undefined and string. So I don't want to change nullability behavior, I want to change optional behavior to exclude nullability.

vojtechhabarta commented 3 years ago

Aha, ok. I am afraid currently this is not possible. But with Include.NON_NULL (somehow configured also in typescript-generator) this null could be eliminated as it is done for undefined.

d00mw01f commented 3 years ago

Ok, thanks for the response. I would be happy to use this feature sometime.