MariusAlch/json-to-ts

null is not handled correctly

Closed this issue · 2 comments

mknj commented

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 commented

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?

Fixed with recent commits