Skip to content

Commit 147efdd

Browse files
authored
narrow AnyType to StringType in mapped types (#2412)
1 parent 53f3f81 commit 147efdd

File tree

7 files changed

+49
-1
lines changed

7 files changed

+49
-1
lines changed

src/NodeParser/MappedTypeNodeParser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { NodeParser } from "../NodeParser.js";
44
import { Context } from "../NodeParser.js";
55
import type { SubNodeParser } from "../SubNodeParser.js";
66
import { AnnotatedType } from "../Type/AnnotatedType.js";
7+
import { AnyType } from "../Type/AnyType.js";
78
import { ArrayType } from "../Type/ArrayType.js";
89
import type { BaseType } from "../Type/BaseType.js";
910
import { DefinitionType } from "../Type/DefinitionType.js";
@@ -64,7 +65,8 @@ export class MappedTypeNodeParser implements SubNodeParser {
6465
if (
6566
keyListType instanceof StringType ||
6667
keyListType instanceof NumberType ||
67-
keyListType instanceof SymbolType
68+
keyListType instanceof SymbolType ||
69+
keyListType instanceof AnyType
6870
) {
6971
// Key type widens to `string`
7072
const type = this.childNodeParser.createType(node.type!, this.createSubContext(node, keyListType, context));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assertValidSchema } from "../../utils";
2+
import { test } from "node:test";
3+
4+
test("type-mapped-any", assertValidSchema("type-mapped-any", "MyObject"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MyObject = Record<any, string>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$ref": "#/definitions/MyObject",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"definitions": {
5+
"MyObject": {
6+
"additionalProperties": {
7+
"type": "string"
8+
},
9+
"type": "object"
10+
}
11+
}
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assertValidSchema } from "../../utils";
2+
import { test } from "node:test";
3+
4+
test("type-mapped-template-literal", assertValidSchema("type-mapped-template-literal", "MyObject"));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type AorB = "A" | "B";
2+
3+
export type MyObject = Record<`letter-${AorB}`, string>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$ref": "#/definitions/MyObject",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"definitions": {
5+
"MyObject": {
6+
"additionalProperties": false,
7+
"properties": {
8+
"letter-A": {
9+
"type": "string"
10+
},
11+
"letter-B": {
12+
"type": "string"
13+
}
14+
},
15+
"required": [
16+
"letter-A",
17+
"letter-B"
18+
],
19+
"type": "object"
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)