glideapps / glideapps/quicktype

[Typescript, JSON Schema] No union type with anyOf

Open
#1,338 5 comments 5 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

Using quicktype in combination with JSON Schema and typescript leads to the following issue:

_anyOf_ does not convert to a union type in some cases.

E.g.

```
{
"$ref": "#/definitions/TestObject",
"definitions": {
"TestObject": {
"type": "object",
"properties": {
"firstProperty": {
"anyOf": [{ "$ref": "#/definitions/MyObjectA" }, { "$ref": "#/definitions/MyObjectB" }]
},
"secondProperty": {
"anyOf": [
{ "type": "array", "items": { "$ref": "#/definitions/MyObjectA" } },
{
"$ref": "#/definitions/MyObjectB"
}
]
}
}
},
"MyObjectA": {
"type": "object",
"properties": {
"a": { "type": "string" },
"b": { "type": "number" },
"c": { "type": "boolean" }
}
},
"MyObjectB": {
"type": "object",
"properties": {
"d": { "type": "string" },
"e": { "type": "number" },
"f": { "type": "boolean" }
}
}
}
}
```

**Expected:**

```
export interface TestSchema {
firstProperty?: MyObjectA | MyObjectB;
secondProperty?: MyObjectA[] | MyObjectB;
}

export interface MyObjectA {
a?: string;
b?: number;
c?: boolean;
}

export interface MyObjectB {
d?: string;
e?: number;
f?: boolean;
}
```

**Actual result:**

```
export interface TestSchema {
firstProperty?: MyObject;
secondProperty?: MyObjectA[] | MyObjectB;
}

export interface MyObject {
a?: string;
b?: number;
c?: boolean;
d?: string;
e?: number;
f?: boolean;
}

export interface MyObjectA {
a?: string;
b?: number;
c?: boolean;
}

export interface MyObjectB {
d?: string;
e?: number;
f?: boolean;
}
```

The union in the _secondProperty_ (array with the object type) works as expected. However the union with the two object types generates a combined interface with a weaker type than the expected union type.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.