diff --git a/src/core/astBuilder.ts b/src/core/astBuilder.ts index 29df675..6385600 100644 --- a/src/core/astBuilder.ts +++ b/src/core/astBuilder.ts @@ -48,6 +48,15 @@ export function buildStringLiteralTypeNode(s: string): ts.LiteralTypeNode { return ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(s)); } export function buildNumericLiteralTypeNode(n: string): ts.LiteralTypeNode { + if (!isNaN(parseFloat(n)) && parseFloat(n) < 0) { + return ts.factory.createLiteralTypeNode( + ts.factory.createPrefixUnaryExpression( + ts.SyntaxKind.MinusToken, + ts.factory.createNumericLiteral(Math.abs(parseFloat(n))), + ), + ); + } + return ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(n)); } export function buildBooleanLiteralTypeNode(b: boolean): ts.LiteralTypeNode { diff --git a/test/simple_schema_test.ts b/test/simple_schema_test.ts index c8db112..1313464 100644 --- a/test/simple_schema_test.ts +++ b/test/simple_schema_test.ts @@ -251,6 +251,31 @@ describe('simple schema test', () => { numbers?: "001" | "002" | "003" | "004" | "005" | "006" | "007" | "008" | "009" | "010"; } } +`; + assert.strictEqual(result, expected, result); + }); + it('positive and negative number type enum schema', async () => { + const schema: JsonSchemaDraft04.Schema = { + id: '/test/positive_negative_number_type', + type: 'object', + properties: { + numbers: { + enum: [1, -1], + example: '1', + }, + }, + }; + const result = await dtsgenerator({ contents: [parseSchema(schema)] }); + + const expected = `declare namespace Test { + export interface PositiveNegativeNumberType { + /** + * example: + * 1 + */ + numbers?: 1 | -1; + } +} `; assert.strictEqual(result, expected, result); });