镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Literal enums #10604

Description

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"
}

Activity

  1. mhegazy commented on Aug 30, 2016

    @mhegazy
    Contributor

    Duplicate of #3192

  2. kitsonk commented on Aug 30, 2016

    @kitsonk
    Contributor

    You mean #7642 right (of which #9407 resolved it)? #3192 was string literals.

  3. mhegazy commented on Aug 30, 2016

    @mhegazy
    Contributor

    my bad, it should be #1206.

    #7642 does handle some of the cases, but there still a need for a string/symbol enum.

  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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