File tree 3 files changed +42
-7
lines changed
test/valid-data/type-union
3 files changed +42
-7
lines changed Original file line number Diff line number Diff line change @@ -55,12 +55,18 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter {
55
55
appendTypeValues ( type , typeValues ) ;
56
56
}
57
57
58
- const schema = {
59
- type : toEnumType ( Array . from ( typeNames ) ) ,
60
- enum : Array . from ( typeValues ) ,
61
- } ;
62
-
63
- return preserveLiterals ? { anyOf : [ { type : "string" } , schema ] } : schema ;
58
+ const schema =
59
+ typeNames . size === 1 && typeValues . size === 1
60
+ ? {
61
+ type : toEnumType ( Array . from ( typeNames ) ) ,
62
+ const : Array . from ( typeValues ) [ 0 ] ,
63
+ }
64
+ : {
65
+ type : toEnumType ( Array . from ( typeNames ) ) ,
66
+ enum : Array . from ( typeValues ) ,
67
+ } ;
68
+
69
+ return hasString ? { anyOf : [ { type : "string" } , schema ] } : schema ;
64
70
}
65
71
66
72
public getChildren ( ) : BaseType [ ] {
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ type MyType4 = "s" | 1;
6
6
type MyType5 = "s" | 1 [ ] ;
7
7
type MyType6 = ( "s" | 1 ) [ ] ;
8
8
9
+ type MyType7 = string | true ;
10
+ type MyType8 = "s" | true ;
11
+
9
12
export interface TypeUnion {
10
13
var1 : MyType1 ;
11
14
var2 : MyType2 ;
@@ -14,4 +17,7 @@ export interface TypeUnion {
14
17
var4 : MyType4 ;
15
18
var5 : MyType5 ;
16
19
var6 : MyType6 ;
20
+
21
+ var7 : MyType7 ;
22
+ var8 : MyType8 ;
17
23
}
Original file line number Diff line number Diff line change 70
70
]
71
71
},
72
72
"type" : " array"
73
+ },
74
+ "var7" : {
75
+ "anyOf" : [
76
+ {
77
+ "type" : " string"
78
+ },
79
+ {
80
+ "const" : true ,
81
+ "type" : " boolean"
82
+ }
83
+ ]
84
+ },
85
+ "var8" : {
86
+ "enum" : [
87
+ " s" ,
88
+ true
89
+ ],
90
+ "type" : [
91
+ " string" ,
92
+ " boolean"
93
+ ]
73
94
}
74
95
},
75
96
"required" : [
78
99
" var3" ,
79
100
" var4" ,
80
101
" var5" ,
81
- " var6"
102
+ " var6" ,
103
+ " var7" ,
104
+ " var8"
82
105
],
83
106
"type" : " object"
84
107
}
You can’t perform that action at this time.
0 commit comments