You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Value extracting from a function returning a string enum may turn it into an character arrayΒ #57077
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
RyanCavanaugh commentedon Jan 16, 2024
This isn't how JavaScript works. TS string enum values are just strings, and destructure the same as strings.
typescript-bot commentedon Jan 19, 2024
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.