Skip to content

Literal enums #10604

Closed
Closed
@PanayotCankov

Description

@PanayotCankov

We are using string enums a lot. The following example is what gets pretty close:

/**
 * Allowed directions.
 */
export declare type FlexDirection = "row" | "column";
export namespace FlexDirection {
    /**
     * Align items in a row.
     */
    export const ROW: "row" = "row";

    /**
     * Align items in a column.
     */
    export const COLUMN: "column" = "column";
}

declare function go(direction: FlexDirection); // Hover "FlexDirection to see docs.
go(FlexDirection.ROW); // Hover "ROW" to see docs.
go("row");

Is there a better way to add documentation to a string literal type? Sometimes the meaning of the values is not all that visible, or requires examples. The only thing I miss in a literal type such as "row" | "column" at the moment is I can not easily associate docs with the 2 values.

Does it sound feasible to add a shorthand syntax for this similar to:

/**
 * Allowed directions.
 */
export literal /* whatever the keywords */ enum FlexDirection {
    /**
     * Align items in a row.
     */
    ROW = "row",

    /**
     * Align items in a column.
     */
    COLUMN = "column"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions