Skip to content

fix: support enums/unions as record indexed access keys #2165

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

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion src/NodeParser/IndexedAccessTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UnionType } from "../Type/UnionType.js";
import { derefType } from "../Utils/derefType.js";
import { getTypeByKey } from "../Utils/typeKeys.js";
import { LogicError } from "../Error/Errors.js";
import { EnumType } from "../Type/EnumType.js";

export class IndexedAccessTypeNodeParser implements SubNodeParser {
public constructor(
Expand Down Expand Up @@ -58,7 +59,8 @@ export class IndexedAccessTypeNodeParser implements SubNodeParser {
return new NeverType();
}

const indexTypes = indexType instanceof UnionType ? indexType.getTypes() : [indexType];
const indexTypes =
indexType instanceof UnionType || indexType instanceof EnumType ? indexType.getTypes() : [indexType];
const propertyTypes = indexTypes.map((type) => {
if (!(type instanceof LiteralType || type instanceof StringType || type instanceof NumberType)) {
throw new LogicError(
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/typeKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function getTypeByKey(type: BaseType, index: LiteralType | StringType | N
}
if (type instanceof ObjectType) {
if (index instanceof LiteralType) {
const property = type.getProperties().find((it) => it.getName() === index.getValue());
const property = type.getProperties().find((it) => it.getName() === index.getName());
if (property) {
const propertyType = property.getType();
if (propertyType === undefined) {
Expand Down
2 changes: 2 additions & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ describe("valid-data-type", () => {
it("type-indexed-access-object-1", assertValidSchema("type-indexed-access-object-1", "MyType"));
it("type-indexed-access-object-2", assertValidSchema("type-indexed-access-object-2", "MyType"));
it("type-indexed-access-keyof", assertValidSchema("type-indexed-access-keyof", "MyType"));
it("type-indexed-access-object-key-enum", assertValidSchema("type-indexed-access-object-key-enum", "Value"));
it("type-indexed-access-object-key-union", assertValidSchema("type-indexed-access-object-key-union", "Value"));
it("type-indexed-circular-access", assertValidSchema("type-indexed-circular-access", "*"));
it("type-indexed-circular", assertValidSchema("type-indexed-circular", "MyType"));
it("type-keyof-tuple", assertValidSchema("type-keyof-tuple", "MyType"));
Expand Down
11 changes: 11 additions & 0 deletions test/valid-data/type-indexed-access-object-key-enum/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum Variant {
A = 0,
B = "STR",
}

type KVMap = {
[Variant.A]: boolean;
[Variant.B]: number;
};

export type Value = KVMap[Variant];
12 changes: 12 additions & 0 deletions test/valid-data/type-indexed-access-object-key-enum/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$ref": "#/definitions/Value",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Value": {
"type": [
"boolean",
"number"
]
}
}
}
8 changes: 8 additions & 0 deletions test/valid-data/type-indexed-access-object-key-union/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Variant = 0 | "ONE";

type KVMap = {
0: boolean;
ONE: number;
};

export type Value = KVMap[Variant];
12 changes: 12 additions & 0 deletions test/valid-data/type-indexed-access-object-key-union/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$ref": "#/definitions/Value",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Value": {
"type": [
"boolean",
"number"
]
}
}
}
Loading