Skip to content

Value extracting from a function returning a string enum may turn it into an character arrayΒ #57077

Closed as not planned

Description

@NoThrottle

πŸ”Ž Search Terms

Enum function union return

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about enums

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.3#code/MYewdgzgLgBApmArgWxgUQB4ENkAcA2caSqA3gFAxUwBqW+icAjDALwwBEAYgJYBO0WvUYcANJWp0GcAExtOAZTigwAEyHSxEqlMYBmeRwAqAC37rdcLQF9y5AGaIwwKD3DxseQlycu3YAAoASgAuGABtTBwCIhJRGGg+HjAAcwBdGAAfdE8Y4hQYCmoYPjgoRD4wHOjCfOQAOksZAG5yW3IAeg6YAGEsXHLSmCgTOBKyirA4dXKY+EJkBCgIchVBcIQUS3jE5JTLDPY4XO9fV3Bg1s7ugFUIMZGx4H7B6fm4RbBl1fAIEEJ6vgQCkAhwAEoTSpvOoaRghMTwEiWIKtNb-OCA4GgiGDKbqBRQJKpWFweE7Ql7ZHNIA

πŸ’» Code

const enum ExampleEnum {
Value1 = "First Value",
Value2 = "Second Value",
Value3 = "Third Value",
}

function exampleFunction(): [ExampleEnum, string] | ExampleEnum {
return ExampleEnum.Value2;
}

// Capture the returned tuple elements
const [enumValue, stringValue] = exampleFunction();

// Use the captured elements
console.log("Returned Enum Value:", enumValue);
console.log("Returned String Value:", stringValue);

πŸ™ Actual behavior

Console logs the following lines:

S
e

The value extraction turned the returned "Second Value" to a char array.

πŸ™‚ Expected behavior

Second Value
null

Since the function is returning either a [enum, string] or just an enum, and the function returns just an enum, the extracted value should just fill enumValue and keep stringValue to null.

Having the enum's value be separated into a char array with each char assigned to one of the variables in order is confusing and doesn't seem to have any value.

Additional information about the issue

I think the error occurs as the string value is passed instead of the ExampleEnum.member. Passing the ExampleEnum as a return when the return type is the enum type should not return its value.

This should not break anything as dissecting an enum's string value in to individual characters via extraction is a weird and hacky way of turning a string into an array.

Activity

  1. RyanCavanaugh commented on Jan 16, 2024

    @RyanCavanaugh
    Member

    This isn't how JavaScript works. TS string enum values are just strings, and destructure the same as strings.

  2. typescript-bot commented on Jan 19, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  3. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions