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

Cannot use enum or union as Record Index type #2164

Open
grddavies opened this issue Jan 31, 2025 · 2 comments · May be fixed by #2165
Open

Cannot use enum or union as Record Index type #2164

grddavies opened this issue Jan 31, 2025 · 2 comments · May be fixed by #2165

Comments

@grddavies
Copy link

Hi,

Great library!

I think I've found a limitation that you cannot use enums and unions as an index for a Record type.

Repro:

Union

type Variant = 0 | "ONE";

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

export type Value = KVMap[KeyType];
//          ^? `string | number`

Expected:

{
  "$ref": "#/definitions/Value",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Value": {
      "type": [
        "boolean",
        "number"
      ]
    }
  }
}

Actual:

 TSJ - 104: Invalid index "0" in type "structure-527321465-39-76-527321465-25-77-527321465-0-115"

8 export type Value = KVMap[Variant];

Enum

enum Variant {
    A = 0,
    B = "STR",
}

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

export type Value = KVMap[KeyType];
//          ^? `string | number`

Expected:

{
  "$ref": "#/definitions/Value",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Value": {
      "type": [
        "boolean",
        "number"
      ]
    }
  }
}

Actual:

 TSJ - 104: Unexpected type "enum-2098387231-0-42-2098387231-0-150" (expected "LiteralType.js" or "StringType.js" or
 "NumberType.js")

11 export type Value = KVMap[Variant];

I'm happy to try put together a PR for this

@domoritz
Copy link
Member

Thanks for the issue and looking into a fix!

@cjlawson02
Copy link

+1 on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants