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

as keyword for enum in typescript?

I have this type

type Abc = 'a' | 'b' | 'c'

how can I make myObj.something type Abc?

I can think of valueOf:

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

const myObj = {
    something: ['a', 'b', 'c'] as valueOf Abc
}

but I wonder why it doesn’t work

https://www.typescriptlang.org/play?#code/C4TwDgpgBAggRgYygXigcgIZqgH3XbPNBNAKFEigDcMAbAVwgHsAzAHgBUA+FKDgbQDWEEKz4BdUqQRMAdgGdgUALYgA8nABWvAN6koBqPKbKIwABYBLWQHMAXFH6Y0AGnyv0JcVAzzqdRjV2eAQuUgBfIA

>Solution :

in your case you are trying to set ['a', 'b', 'c'] (which is an array) as Abc. You should define myObj.something as an array of type Abc, with Abc[]

const myObj = {
    something: ['a', 'b', 'c'] as Abc[]
}
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