Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Given a type, how can I get the options in an array?

In typescript how can I get an array of string options?

export type KeyboardType = 'default' | 'email-address' | 'numeric' | 'phone-pad' | 'number-pad' | 'decimal-pad';

const keyboardTypes = x; // ['default', 'email-address', 'numeric', 'phone-pad', 'number-pad', 'decimal-pad']

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can’t, since types only exist at compile/transpile time.

You can do the opposite, however, where you have an array of values keyboardTypes (make sure to add as const) and then define the type KeyboardType as the values of that array.

const keyboardTypes = ['default', 'email-address', 'numeric', 'phone-pad', 'number-pad', 'decimal-pad'] as const;

export type KeyboardType = typeof keyboardTypes[number];
         // ^? 'default' | 'email-address' | 'numeric' | 'phone-pad' | 'number-pad' | 'decimal-pad'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading