Skip to content

Commit 45e61da

Browse files
schanidomoritz
authored andcommitted
Don't treat string enum members as numbers. Fixes YousefED#186
1 parent 5e420b5 commit 45e61da

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

test/programs/enums-string/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
enum Enum {
22
X = "x" as any,
3-
Y = "y" as any
3+
Y = "y" as any,
4+
Z = "123" as any
45
}
56

67
interface MyObject {

test/programs/enums-string/schema.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"definitions": {
44
"Enum": {
55
"enum": [
6+
"123",
67
"x",
78
"y"
89
],

typescript-json-schema.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,16 @@ export class JsonSchemaGenerator {
240240
if (str === undefined) {
241241
str = (typ as any).text;
242242
}
243-
if (typ.flags & ts.TypeFlags.EnumLiteral) {
243+
if (typ.flags & ts.TypeFlags.StringLiteral) {
244+
return str;
245+
} else if (typ.flags & ts.TypeFlags.BooleanLiteral) {
246+
return (typ as any).intrinsicName === "true";
247+
} else if (typ.flags & ts.TypeFlags.EnumLiteral) {
244248
// or .text for old TS
245249
let num = parseFloat(str as string);
246250
return isNaN(num) ? str : num;
247-
} else if (typ.flags & ts.TypeFlags.StringLiteral) {
248-
return str;
249251
} else if (typ.flags & ts.TypeFlags.NumberLiteral) {
250252
return parseFloat(str as string);
251-
} else if (typ.flags & ts.TypeFlags.BooleanLiteral) {
252-
return (typ as any).intrinsicName === "true";
253253
}
254254
return undefined;
255255
}

0 commit comments

Comments
 (0)