json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Empty objects produce broken schema (empty interface)

Open andrewbaxter opened this issue 3 years ago • 3 comments

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.

andrewbaxter avatar Sep 18 '22 08:09 andrewbaxter

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.

GabenGar avatar Dec 14 '22 13:12 GabenGar

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.

andrewbaxter avatar Dec 14 '22 14:12 andrewbaxter