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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support empty mapped types #1261

Merged
merged 2 commits into from May 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/NodeParser/MappedTypeNodeParser.ts
Expand Up @@ -7,6 +7,7 @@ import { ArrayType } from "../Type/ArrayType";
import { BaseType } from "../Type/BaseType";
import { EnumType, EnumValue } from "../Type/EnumType";
import { LiteralType } from "../Type/LiteralType";
import { NeverType } from "../Type/NeverType";
import { NumberType } from "../Type/NumberType";
import { ObjectProperty, ObjectType } from "../Type/ObjectType";
import { StringType } from "../Type/StringType";
Expand Down Expand Up @@ -58,6 +59,10 @@ export class MappedTypeNodeParser implements SubNodeParser {
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) {
return new ObjectType(id, [], [], false);
} else if (keyListType === undefined) {
return new ObjectType(id, [], [], false);
} else {
throw new LogicError(
// eslint-disable-next-line max-len
Expand Down
2 changes: 2 additions & 0 deletions test/valid-data-type.test.ts
Expand Up @@ -95,6 +95,8 @@ describe("valid-data-type", () => {
it("type-mapped-exclude", assertValidSchema("type-mapped-exclude", "MyObject", "extended"));
it("type-mapped-double-exclude", assertValidSchema("type-mapped-double-exclude", "MyObject", "extended"));
it("type-mapped-symbol", assertValidSchema("type-mapped-symbol", "MyObject"));
it("type-mapped-never", assertValidSchema("type-mapped-never", "MyObject"));
it("type-mapped-empty-exclude", assertValidSchema("type-mapped-empty-exclude", "MyObject"));
it("type-mapped-annotated-string", assertValidSchema("type-mapped-annotated-string", "ExchangeRate", "extended"));

it("type-conditional-simple", assertValidSchema("type-conditional-simple", "MyObject"));
Expand Down
1 change: 1 addition & 0 deletions test/valid-data/type-mapped-empty-exclude/main.ts
@@ -0,0 +1 @@
export type MyObject = { [K in Exclude<"key", "key">]: never };
10 changes: 10 additions & 0 deletions test/valid-data/type-mapped-empty-exclude/schema.json
@@ -0,0 +1,10 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"type": "object"
}
}
}
1 change: 1 addition & 0 deletions test/valid-data/type-mapped-never/main.ts
@@ -0,0 +1 @@
export type MyObject = { [K in never]: never };
10 changes: 10 additions & 0 deletions test/valid-data/type-mapped-never/schema.json
@@ -0,0 +1,10 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"type": "object"
}
}
}