Skip to content

"allOf" + "additionalProperties false" with interface extensions #4

@arvind

Description

@arvind
export interface A {a: string;}
export interface B extends A {b?: string;}

generates

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "B": {
      "allOf": [
        {
          "type": "object",
          "properties": {
            "b": {
              "type": "string"
            }
          },
          "required": [],
          "additionalProperties": false
        },
        {
          "$ref": "#/definitions/A"
        }
      ]
    },
    "A": {
      "type": "object",
      "properties": {
        "a": {
          "type": "string"
        }
      },
      "required": [
        "a"
      ],
      "additionalProperties": false
    }
  },
  "$ref": "#/definitions/B"
}

The following object type checks correctly with TypeScript. However, it does not validate against the JSON schema. This is because B uses an allOf where each branch has additionalProperties: false (i.e., it fails the first branch).

const foobar:B = {a: 'foo'};

I believe ts2json should be consolidating these definitions together. Note: Switching allOf to anyOf doesn't yield a correct schema either ({b: 'foo'} correctly validates for instance).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions