Skip to content

Commit aad5ea9

Browse files
Noah Chendomoritz
Noah Chen
authored andcommitted
Fix empty string literal error
1 parent 71f2242 commit aad5ea9

File tree

5 files changed

+11
-6
lines changed

5 files changed

+11
-6
lines changed

Diff for: test/programs/string-literals-inline/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class MyObject {
2-
foo: "ok" | "fail" | "abort";
2+
foo: "ok" | "fail" | "abort" | "";
33
bar: "ok" | "fail" | "abort" | string;
44
}

Diff for: test/programs/string-literals-inline/schema.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"foo": {
88
"enum": [
9+
"",
910
"abort",
1011
"fail",
1112
"ok"

Diff for: test/programs/string-literals/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type result = "ok" | "fail" | "abort";
1+
type result = "ok" | "fail" | "abort" | "";
22

33
class MyObject {
44
foo: result;

Diff for: test/programs/string-literals/schema.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"definitions": {
44
"result": {
55
"enum": [
6+
"",
67
"abort",
78
"fail",
89
"ok"

Diff for: typescript-json-schema.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,18 @@ export class JsonSchemaGenerator {
236236
}
237237

238238
private extractLiteralValue(typ: ts.Type): PrimitiveType | undefined {
239+
let str = (<ts.LiteralType>typ).value;
240+
if (str === undefined) {
241+
str = (typ as any).text;
242+
}
239243
if (typ.flags & ts.TypeFlags.EnumLiteral) {
240244
// or .text for old TS
241-
let str = (<ts.LiteralType>typ).value || (typ as any).text;
242-
let num = parseFloat(str);
245+
let num = parseFloat(str as string);
243246
return isNaN(num) ? str : num;
244247
} else if (typ.flags & ts.TypeFlags.StringLiteral) {
245-
return (<ts.LiteralType>typ).value || (typ as any).text;
248+
return str;
246249
} else if (typ.flags & ts.TypeFlags.NumberLiteral) {
247-
return parseFloat((<ts.LiteralType>typ).value || (typ as any).text);
250+
return parseFloat(str as string);
248251
} else if (typ.flags & ts.TypeFlags.BooleanLiteral) {
249252
return (typ as any).intrinsicName === "true";
250253
}

0 commit comments

Comments
 (0)