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

additionalProperties union clashes with defined properties #402

Open
joscha opened this issue Aug 23, 2021 · 3 comments
Open

additionalProperties union clashes with defined properties #402

joscha opened this issue Aug 23, 2021 · 3 comments
Labels
bug external This is an issue with a library that we depend on

Comments

@joscha
Copy link

joscha commented Aug 23, 2021

A schema of:

{
  "type": "object",
  "properties": {
    "$schema": {
      "description": "The JSON schema reference.",
      "type": "string"
    },
  },
  "additionalProperties": {
   "type": "object"
  }
}

resolves to:

export interface MySchema {
  /**
   * The JSON schema reference.
   */
  $schema?: string;
  [k: string]: {
    [k: string]: unknown;
  };
}

which is an incompatible type, as $schema clearly doesn't follow { [k: string]: unknown }.
You can see the error in the typescript playground.

I encountered this here: https://dprint.dev/schemas/v0.json which is the schema for the dprint configuration. (cc @dsherret)

A potential fix is to generate additionalProperties as:

  [k: string]: {
    [k: string]: unknown;
  } | unknown;

Test code:

const { compile } = require('json-schema-to-typescript');

// or, compile a JS object
let mySchema = {
  "type": "object",
  "properties": {
    "$schema": {
      "description": "The JSON schema reference.",
      "type": "string"
    },
  },
  additionalProperties: {
   type: "object"
  }
}
compile(mySchema, 'MySchema')
  .then(console.log)
@bcherny
Copy link
Owner

bcherny commented Sep 12, 2021

This is tricky. We want to say "additional properties should be X, but not Y". I worry that unioning with unknown is too permissive.

This is a TypeScript feature request, tracked here: microsoft/TypeScript#17867.

@bcherny bcherny added bug external This is an issue with a library that we depend on labels Sep 12, 2021
@joscha
Copy link
Author

joscha commented Sep 12, 2021 via email

@fabrykowski
Copy link

Hi! Any chance of reviewing PR #383 which might close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug external This is an issue with a library that we depend on
Projects
None yet
Development

No branches or pull requests

3 participants