Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kityan opened this issue Oct 29, 2023 · 3 comments
Open

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

kityan opened this issue Oct 29, 2023 · 3 comments

Comments

@kityan
Copy link

kityan commented Oct 29, 2023

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
Copy link
Owner

bcherny commented Oct 29, 2023

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
Copy link
Author

kityan commented Oct 30, 2023

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
Copy link
Author

kityan commented Oct 30, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants