Skip to content

Commit

Permalink
feat: support empty mapped types (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
daanboer committed May 26, 2022
1 parent 42ce3b6 commit 0b04c7d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
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 @@ -96,6 +96,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"
}
}
}

0 comments on commit 0b04c7d

Please sign in to comment.