Skip to content

Commit 53f3f81

Browse files
authored
fix: mapped number types in Record syntax (#2418)
1 parent 2e6950d commit 53f3f81

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/NodeParser/MappedTypeNodeParser.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ export class MappedTypeNodeParser implements SubNodeParser {
5252
return new ObjectType(id, [], this.getProperties(node, new UnionType([keyListType]), context), false);
5353
}
5454

55+
const maybeUnionType = this.childNodeParser.createType(
56+
node.type!,
57+
this.createSubContext(node, keyListType, context),
58+
);
59+
if (maybeUnionType instanceof UnionType && constraintType?.getId() === "number") {
60+
// Then we turn it into an array
61+
return maybeUnionType instanceof NeverType ? new NeverType() : new ArrayType(maybeUnionType);
62+
}
63+
5564
if (
5665
keyListType instanceof StringType ||
5766
keyListType instanceof NumberType ||
5867
keyListType instanceof SymbolType
5968
) {
60-
if (constraintType?.getId() === "number") {
61-
const type = this.childNodeParser.createType(
62-
node.type!,
63-
this.createSubContext(node, keyListType, context),
64-
);
65-
return type instanceof NeverType ? new NeverType() : new ArrayType(type);
66-
}
6769
// Key type widens to `string`
6870
const type = this.childNodeParser.createType(node.type!, this.createSubContext(node, keyListType, context));
6971
// const resultType = type instanceof NeverType ? new NeverType() : new ObjectType(id, [], [], type);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { assertValidSchema } from "../../utils";
22
import { test } from "node:test";
33

4-
test("type-mapped-number", assertValidSchema("type-mapped-number", "MyObject"));
4+
test("type-mapped-number", assertValidSchema("type-mapped-number", "*"));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
export type MyObject = Record<number, string>;
2+
3+
export type MyObjectKVSyntax = {
4+
[key: number]: string;
5+
};
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
{
2-
"$ref": "#/definitions/MyObject",
32
"$schema": "http://json-schema.org/draft-07/schema#",
43
"definitions": {
54
"MyObject": {
6-
"items": {
5+
"additionalProperties": {
76
"type": "string"
87
},
9-
"type": "array"
8+
"type": "object"
9+
},
10+
"MyObjectKVSyntax": {
11+
"additionalProperties": {
12+
"type": "string"
13+
},
14+
"type": "object"
1015
}
1116
}
1217
}

0 commit comments

Comments
 (0)