Skip to content

Cannot use enum or union as Record Index type #2164

Open
@grddavies

Description

@grddavies

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions