Skip to content

Commit e36516a

Browse files
authored
fix: Deal with empty object generation (#299)
Closed #258
1 parent e6cdf21 commit e36516a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ describe("schemaToTypeAliasDeclaration", () => {
483483
`);
484484
});
485485

486+
it("should generate an empty object", () => {
487+
const schema: SchemaObject = {
488+
type: "object",
489+
additionalProperties: false,
490+
};
491+
492+
expect(printSchema(schema)).toMatchInlineSnapshot(
493+
`"export type Test = Record<string, never>;"`
494+
);
495+
});
496+
486497
it("should generate an object with additional properties", () => {
487498
const schema: SchemaObject = {
488499
type: "object",

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,15 @@ export const getType = (
223223
return withNullable(f.createTypeLiteralNode([]), schema.nullable);
224224
}
225225

226-
if (
227-
!schema.properties /* free form object */ &&
228-
!schema.additionalProperties
229-
) {
226+
if (!schema.properties && !schema.additionalProperties) {
230227
return withNullable(
231228
f.createTypeReferenceNode(f.createIdentifier("Record"), [
232229
f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
233-
f.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
230+
f.createKeywordTypeNode(
231+
schema.additionalProperties === false
232+
? ts.SyntaxKind.NeverKeyword // empty object
233+
: ts.SyntaxKind.AnyKeyword // free form object
234+
),
234235
]),
235236
schema.nullable
236237
);

0 commit comments

Comments
 (0)