Skip to content

Include const enum members as possible literal types #7642

Closed
@dead-claudia

Description

@dead-claudia

String literals are already valid types now. This is very nice, but I would like the ability to use const enum members to the same effect. They don't change, they take a lot less memory, and they're faster to check. Plus, you have a clearer distinction of what the type is, where strings are pretty free-form at times.

// Now
type NodeType = "start" | "end"; // etc.
interface Node {
  type: NodeType;
}

interface StartNode {
  type: "start";
}

// What I'd like:
const enum NodeType {
  Start,
  End,
  // etc.
}

interface Node {
  type: NodeType;
}

interface StartNode {
  type: NodeType.Start;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions