bcherny / json-schema-to-typescript

Compile JSON Schema to TypeScript type declarations
https://bcherny.github.io/json-schema-to-typescript-browser/
MIT License
2.95k stars 392 forks source link

Convert empty object to Record<string,never> #557

Open kityan opened 1 year ago

kityan commented 1 year ago

For now schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "MyAPI.test.Params",
  "title": "Params",
  "description": "MyAPI.test() params",
  "type": "object",
  "properties": {
  },
  "additionalProperties": false,
  "required": []
}

will be converted to

/**
 * MyAPI.test() params
 */
export interface Params {}

This leads to any props allowed in autocomplete:

MyAPI.test({ /* any prop here */})

Also, if API uses validation based on JSON schema it expects params to be strictly empty object. I believe it's better to convert to:

/**
 * MyAPI.test() params
 */
export type Params = Record<string,never>
bcherny commented 1 year ago

We have a test case covering this. But for the life of me I don't remember where in the spec I saw that this is the correct behavior.

Reading it now, the JSON-Schema Spec is a bit ambiguous: a schema that omits properties is equivalent to TypeScript's any (in that, every value is valid), but it's unclear whether properties: {} is also equivalent to any, or if it should be strictly interpreted as "nothing matches".

Would love a second pair of eyes, if you can help sleuth through the various versions of the JSON-Schema Spec to dig up the correct behavior.

kityan commented 1 year ago

I suppose that a schema that omits properties is equivalent to TypeScript's any only if additionalProperties in schema resolved to true. Of course this is a very rare case. Here is the case: I have ajv validator in my json-rpc server pipeline. And it fails to validate request with any properties in args against the schema in my first message. But ts interface produced from this schema allows any properties. It's a bit inconsistent...

UPD: Also this should be consistent with another prop: unevaluatedProperties

kityan commented 1 year ago

Anyway, I've just fixed "the issue" in my code generation pipeline with dirty string replacement: export interface Params {} -> export type Params = Record<string,never> Maybe we just should add some option to the converter? Something like exposeEmptySchemaObjectWithNoAdditionalPropertiesAllowedAs?