Skip to content

Bad error message for const variables used in const enums #28977

Closed
@riggs

Description

@riggs

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms:
const enum initializer ts2474
Code

// lib.ts
const enum STATIC_KEYS {
    ZERO,
    ONE,
    TWO,

    LAST
}

export const STARTING_NUMBER = STATIC_KEYS.LAST;

// consumer.ts
const enum MORE_STATIC_KEYS {
    ONE = STARTING_NUMBER, // error TS2474; STATIC_KEYS.LAST works as expected
    TWO,
    THREE,
}

Expected behavior:
Code compiles with MORE_STATIC_KEYS.ONE === STATIC_KEYS.LAST.

Actual behavior:
error TS2474: In 'const' enum declarations member initializer must be constant expression.
TS recognizes that STARTING_NUMBER has the type STATIC_KEYS.LAST, but doesn't consider it a 'constant expression.'

If this is expected behavior, clarity in the error message would be appreciated.

Playground Link: link

Related Issues:
Multiple discussions about merging/intersecting of const enums, of which the above is my attempted workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions