Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect output of Record with numeric key #1321

Merged
merged 1 commit into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/NodeParser/MappedTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/valid-data/type-mapped-number/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MyObject = Record<number, string>;
12 changes: 12 additions & 0 deletions test/valid-data/type-mapped-number/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": {
"type": "string"
},
"type": "object"
}
}
}