json-schema-to-typescript
json-schema-to-typescript copied to clipboard
Empty objects produce broken schema (empty interface)
I searched a bit but didn't find any issues/discussion that looked relevant, please forgive me if I missed something.
I'm dealing with an api that has "sometype": {} fields sometimes (that is, it requires an empty object) with a definition like
{
"title": "Empty",
"type": "object",
"properties": {},
"additionalProperties": false
}
This produces the invalid ts definition
export interface Empty {}
where I'd expect
export type Empty = Record<string, never>;
Obviously there won't be significant type safety (or unsafety) in these situations, but it will compile/check.
What version of typescript are you running? On 4.9.3 export interface Empty {} is a valid type definition. Also representing your schema with Record<string, never> is wrong, since it implies arbitrary keys, while the schema states there are none.
Sorry, I should have been more clear. It fails ESLint checking with default checks. ESLint suggests Record<string, never>.
The problem is interface Empty {} accepts any values, contrary to additionalProperties: false. Record<string, never>, while obviously a hack, displays the correct type checking properties.
interface X {}
type Y = Record<string, never>;
const x = (_: X) => {};
const y = (_: Y) => {};
x({"hey": "dog"});
y({"hey": "dog"});
x passes, y raises a type checking error.