Skip to content

Commit 1aad358

Browse files
authored
fix: "Unexpected code branch" error when type is a union that includes 'symbol' (#2282)
1 parent 82e83b8 commit 1aad358

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/TypeFormatter/PrimitiveUnionTypeFormatter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { BaseType } from "../Type/BaseType.js";
66
import { BooleanType } from "../Type/BooleanType.js";
77
import { NullType } from "../Type/NullType.js";
88
import { NumberType } from "../Type/NumberType.js";
9-
import { PrimitiveType } from "../Type/PrimitiveType.js";
109
import { StringType } from "../Type/StringType.js";
1110
import { UnionType } from "../Type/UnionType.js";
1211
import { uniqueArray } from "../Utils/uniqueArray.js";
@@ -25,7 +24,15 @@ export class PrimitiveUnionTypeFormatter implements SubTypeFormatter {
2524
}
2625

2726
protected isPrimitiveUnion(type: UnionType): boolean {
28-
return type.getTypes().every((item) => item instanceof PrimitiveType);
27+
return type
28+
.getTypes()
29+
.every(
30+
(item) =>
31+
item instanceof StringType ||
32+
item instanceof NumberType ||
33+
item instanceof BooleanType ||
34+
item instanceof NullType,
35+
);
2936
}
3037

3138
protected getPrimitiveType(item: BaseType): RawTypeName {

test/valid-data-other.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe("valid-data-other", () => {
7676

7777
it("symbol", assertValidSchema("symbol", "MyObject"));
7878
it("unique-symbol", assertValidSchema("unique-symbol", "MyObject"));
79+
it("symbol-union", assertValidSchema("symbol-union", "MyType"));
7980

8081
it("array-min-items-1", assertValidSchema("array-min-items-1", "MyType"));
8182
it("array-min-items-2", assertValidSchema("array-min-items-2", "MyType"));

test/valid-data/symbol-union/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MyType = string | symbol;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$ref": "#/definitions/MyType",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"definitions": {
5+
"MyType": {
6+
"anyOf": [
7+
{
8+
"type": "string"
9+
},
10+
{}
11+
]
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)