json_typegen icon indicating copy to clipboard operation
json_typegen copied to clipboard

Distinction between nullable and optional

Open GerritPlehn opened this issue 2 years ago • 1 comments

Hi! Similar to #21 when using this library to generate TS types, it's often useful to make a distinction between nullable and optional fields. I'm looking for feedback on this PR as I'm very new to Rust. Currently not all tests are passing and before investing more time I'd like to know if the approach I took is ok.

Input

{
  "a": [{ "nullable": 1 }, { "nullable": null }],
  "b": [
    {
      "optional": 1,
      "always": true
    },
    {
      "always": true
    }
  ],
  "c": [
    {
      "optionalNullable": 1,
      "always": true
    },
    {
      "optionalNullable": null,
      "always": true
    },
    {
      "always": true
    }
  ]
}

Before the changes

export interface A {
    nullable?: number;
}

export interface B {
    optional?: number;
    always: boolean;
}

export interface C {
    optionalNullable?: number;
    always: boolean;
}

After the changes

export interface A {
    nullable: number | null;
}

export interface B {
    optional?: number;
    always: boolean;
}

export interface C {
    optionalNullable?: number | null;
    always: boolean;
}

GerritPlehn avatar Jul 15 '23 14:07 GerritPlehn

Thank you for the PR. Looks reasonable at first glance, but it's been a while since I worked on this project, so will need to re-familiarize myself with the project a bit before I consider merging.

evestera avatar Jul 18 '23 22:07 evestera