diff --git a/src/NodeParser/ObjectLiteralExpressionNodeParser.ts b/src/NodeParser/ObjectLiteralExpressionNodeParser.ts index 9e9dbed8f..f5ab4bfac 100644 --- a/src/NodeParser/ObjectLiteralExpressionNodeParser.ts +++ b/src/NodeParser/ObjectLiteralExpressionNodeParser.ts @@ -43,7 +43,7 @@ export class ObjectLiteralExpressionNodeParser implements SubNodeParser { const referenced = this.checker.typeToTypeNode( this.checker.getTypeAtLocation(spread.expression), undefined, - ts.NodeBuilderFlags.NoTypeReduction, + ts.NodeBuilderFlags.NoTruncation, ); if (!referenced) { @@ -63,13 +63,31 @@ export class ObjectLiteralExpressionNodeParser implements SubNodeParser { return []; } - if (!t.name || !("initializer" in t)) { + if (!t.name) { throw new UnknownNodeError(t); } + let type: ts.Node | undefined; + + if (ts.isShorthandPropertyAssignment(t)) { + type = this.checker.typeToTypeNode( + this.checker.getTypeAtLocation(t), + undefined, + ts.NodeBuilderFlags.NoTruncation, + ); + } else if (ts.isPropertyAssignment(t)) { + type = t.initializer; + } else { + type = t; + } + + if (!type) { + throw new ExpectationFailedError("Could not find type for property", t); + } + return new ObjectProperty( t.name.getText(), - this.childNodeParser.createType(t.initializer, context), + this.childNodeParser.createType(type, context), !(t as any).questionToken, ); }); diff --git a/test/valid-data-type.test.ts b/test/valid-data-type.test.ts index 18ae20d21..8e06518fc 100644 --- a/test/valid-data-type.test.ts +++ b/test/valid-data-type.test.ts @@ -144,6 +144,7 @@ describe("valid-data-type", () => { it("lowercase", assertValidSchema("lowercase", "MyType")); it("const-spread", assertValidSchema("const-spread", "MyType")); + it("keyof-typeof-x", assertValidSchema("keyof-typeof-x", "MyType")); it("promise-extensions", assertValidSchema("promise-extensions", "*")); diff --git a/test/valid-data/keyof-typeof-x/main.ts b/test/valid-data/keyof-typeof-x/main.ts new file mode 100644 index 000000000..60a9da8b8 --- /dev/null +++ b/test/valid-data/keyof-typeof-x/main.ts @@ -0,0 +1,3 @@ +const myprop = 0; +export const mymap = { myprop }; +export type MyType = keyof typeof mymap; diff --git a/test/valid-data/keyof-typeof-x/schema.json b/test/valid-data/keyof-typeof-x/schema.json new file mode 100644 index 000000000..594161e05 --- /dev/null +++ b/test/valid-data/keyof-typeof-x/schema.json @@ -0,0 +1,10 @@ +{ + "$ref": "#/definitions/MyType", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyType": { + "const": "myprop", + "type": "string" + } + } +}