Skip to content

Shadowing an enum definition and referencing the outer definition in an initializer results in undefined enum value. #34606

@aweary

Description

@aweary

TypeScript Version: Nightly

Search Terms: enum, shadowing, initializer

Code

// Outer enum
enum Enum { a = 1, b, c };

function foo(): number {
    // Inner enum with the same name
    enum Enum {
        // Initialize this using the value of
        // the outer enum, which is 1
        a = Enum.a
    };
    // This should return a number, but it's undefined! :o
    return Enum.a;
}

Expected behavior:

TypeScript should resolve the outer Enum definition and inline the Enum.a like it would with non-shadowed values.

Actual behavior:

TypeScript does not inline the value so Enum.a is evaluated (to undefined) resulting in a mismatch between the reported type and the runtime value.

Playground Link: http://www.typescriptlang.org/play/?ts=Nightly&ssl=1&ssc=1&pln=13&pc=2#code/PTAEHkFcBcFMCdSwHaQLYCgXtAUVWqAN6gCGoAvKAIwA0oARvQMagC+A3BhgGaTLNoASwD2yUDxEiAFAEoAXKAIMExDKA2gQoAJLJkq7IQDuQ6AAtQF2KADOpNDeQPY6zUbwE1mn1rB6zIVIAGyEALxsLIVtQSFshZABzK3MbADcQyBsRHjdfPxTsmEMCemNzIWZLaJo833IqfHQAOlI6zjrtABUKmNtzEUhggBNQeFhoSHhxcmUEegYYUDMAchj+YdgeBNhhgEJQeRE68cnpzxbSLjYgA

Related Issues: N/A, couldn't find any

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions