Determine the number of enum elements TypeScript

Typescript does not provide a standard method to get the number of enum elements. But given the the implementation of enum reverse mapping, the following works:

Object.keys(ExampleEnum).length / 2;

Note, for string enums you do not divide by two as no reverse mapping properties are generated:

Object.keys(StringEnum).length;

Enums with mixed strings and numeric values can be calculated by:

Object.keys(ExampleEnum).filter(isNaN).length;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)