diff --git a/src/NodeParser/MappedTypeNodeParser.ts b/src/NodeParser/MappedTypeNodeParser.ts index 31233234..1c353ace 100644 --- a/src/NodeParser/MappedTypeNodeParser.ts +++ b/src/NodeParser/MappedTypeNodeParser.ts @@ -43,7 +43,18 @@ export class MappedTypeNodeParser implements SubNodeParser { } else if (keyListType instanceof LiteralType) { // Key type resolves to single known property return new ObjectType(id, [], this.getProperties(node, new UnionType([keyListType]), context), false); - } else if (keyListType instanceof StringType || keyListType instanceof SymbolType) { + } else if ( + keyListType instanceof StringType || + keyListType instanceof NumberType || + keyListType instanceof SymbolType + ) { + if (constraintType?.getId() === "number") { + const type = this.childNodeParser.createType( + node.type!, + this.createSubContext(node, keyListType, context) + ); + return type === undefined ? undefined : new ArrayType(type); + } // Key type widens to `string` const type = this.childNodeParser.createType(node.type!, context); const resultType = type === undefined ? undefined : new ObjectType(id, [], [], type); @@ -54,9 +65,6 @@ export class MappedTypeNodeParser implements SubNodeParser { } } return resultType; - } else if (keyListType instanceof NumberType) { - const type = this.childNodeParser.createType(node.type!, this.createSubContext(node, keyListType, context)); - return type === undefined ? undefined : new ArrayType(type); } else if (keyListType instanceof EnumType) { return new ObjectType(id, [], this.getValues(node, keyListType, context), false); } else if (keyListType instanceof NeverType) { diff --git a/test/valid-data/type-mapped-number/main.ts b/test/valid-data/type-mapped-number/main.ts new file mode 100644 index 00000000..2d18b788 --- /dev/null +++ b/test/valid-data/type-mapped-number/main.ts @@ -0,0 +1 @@ +export type MyObject = Record; diff --git a/test/valid-data/type-mapped-number/schema.json b/test/valid-data/type-mapped-number/schema.json new file mode 100644 index 00000000..cd0c3baa --- /dev/null +++ b/test/valid-data/type-mapped-number/schema.json @@ -0,0 +1,12 @@ +{ + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "additionalProperties": { + "type": "string" + }, + "type": "object" + } + } +}