Skip to content

get array from const enum #38960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wangzhilongh opened this issue Jun 6, 2020 · 8 comments
Closed

get array from const enum #38960

wangzhilongh opened this issue Jun 6, 2020 · 8 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@wangzhilongh
Copy link

I hope there is a way to get an array from const enum.

for example:

const enum gender{
male = 1,
female = 2,
}

I need to there is a way to get an array from the const enum gender, and the result like this:
[1,2]

Thanks a lot!

@wangzhilongh
Copy link
Author

wangzhilongh commented Jun 6, 2020

const enum gender{
male = 1,
female = 2,
}
const genderArray = gender.toArray() // maybe just like this.
console.log(genderArray) // [1,2]

@MartinJohns
Copy link
Contributor

Duplicate of #21391.

@Harpush
Copy link

Harpush commented Jun 6, 2020

I am pretty sure he meant to allow some syntax that like const enum which is replaced with literal strings at runtime will be replaced with the literal array of the const enum options.
So if we have say:

const enum Test {
  A = 'a',
  B = 'b'
}
const values = valueof Test;

It will get compiled to const values = ['a', 'b']

@wangzhilongh
Copy link
Author

Yes. Please implement it.
Thanks!

@fatcerberus
Copy link

It will get compiled to const values = ['a', 'b']

This would be type-directed emit, which is unlikely to be implemented. See point 5 at https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals#non-goals:

  1. [Do not] Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.

@Harpush
Copy link

Harpush commented Jun 8, 2020

@fatcerberus Which means const enum goes against it too... I believe its just an enhancement to the already existing functionality

@RyanCavanaugh RyanCavanaugh added the Unactionable There isn't something we can do with this issue label Jun 8, 2020
@RyanCavanaugh
Copy link
Member

To expedite the triage process, we need everyone to follow the issue template and instructions.

When you clicked "Create New Issue", the issue form was pre-populated with a template and some instructions. We need you to read those instructions completely, follow them, and then fill in all the fields in that template.

We are not able to assist with issues that don't follow the template instructions as they represent a significantly larger amount of work compared to issues which are correctly specified. Thank you for understanding.

@HKhademian
Copy link

enum X { A='A', B='B'};
const enum Y { A='A', B='B'};

every enum is defining a group of related values

we can use them like:

const x:X = X.A;
const y:Y = Y.A;

compiles to

const x = X.A;
const y = 'A';

then it is logical to have also a way to access the whole group of values.
like:
const Xs = Object.values(X);
and (((or any other syntax which doesn't matter at all)))
const Ys = valuesof Y;
then they compile to:
const Xs = Object.values(X);
and
const Ys = ['A','B'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

6 participants