Skip to content

Commit

Permalink
fix: enums containing negative numbers (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmacmunn committed May 16, 2024
1 parent 08ff135 commit e4e099d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,14 @@ export function tsLiteral(value: unknown): ts.TypeNode {
return ts.factory.createIdentifier(JSON.stringify(value)) as unknown as ts.TypeNode;
}
if (typeof value === "number") {
return ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(value));
const literal =
value < 0
? ts.factory.createPrefixUnaryExpression(
ts.SyntaxKind.MinusToken,
ts.factory.createNumericLiteral(Math.abs(value)),
)
: ts.factory.createNumericLiteral(value);
return ts.factory.createLiteralTypeNode(literal);
}
if (typeof value === "boolean") {
return value === true ? TRUE : FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe("transformSchemaObject > number", () => {
[
"enum",
{
given: { type: "number", enum: [50, 100, 200] },
want: "50 | 100 | 200",
given: { type: "number", enum: [-50, 50, 100, 200] },
want: "-50 | 50 | 100 | 200",
// options: DEFAULT_OPTIONS,
},
],
Expand Down

0 comments on commit e4e099d

Please sign in to comment.