json-to-ts icon indicating copy to clipboard operation
json-to-ts copied to clipboard

null is not handled correctly

Open mknj opened this issue 4 years ago • 1 comments

null is treated as a non existing member. This is not correct. i.e.

JSON.parse('{ "something":  [ {"x":1},{"x":"a"},{"x":null} ]}')

returns

{ something: [ { x: 1 }, { x: 'a' }, { x: null } ] }

So

{ "something":  [ {"x":1},{"x":"a"},{"x":null} ]}

should produce

export interface RootObject {
  something: Something[];
}
export interface Something {
  x: number | string | null;
}

and not

export interface RootObject {
  something: Something[];
}
export interface Something {
  x?: number | string;
}

mknj avatar Sep 03 '20 15:09 mknj

it is the same as #5 My problem is that i am creating jsonschema from my ts interface. The jsonschema will complain about "x":null, as x?: number|string means that the value must be number, string or the property must not exist.

Would it be possible to add an option for "null" handling?

mknj avatar Sep 03 '20 16:09 mknj