enum EList {
ITEM_FOO = 'fooData',
ITEM_BAR = 'barData'
}
const lookingForKey = 'ITEM_BAR'
const lookingForValue = 'barData'
// test if `lookingForKey` exists within `EList`
console.log(Object.keys(EList).some((v) => v === lookingForKey))
// test if `lookingForValue` exists within `EList`
console.log(Object.values(EList).some((v) => v === lookingForValue))
// typescript is not executed by browsers, thus typescript's enums don't exist
// at runtime, it's interpreted as a plain js object : it's possible to use
// Object methods like Object.values(), Object.entries(), Object.keys()...
// this enum
enum MyEnum {
FIRST="my first value"
SECOND="second value"
}
// transforms into :
const MyEnumInJs = {
FIRST:"my first value",
SECOND:"second value"
}
Object.values(MyEnum) // returns ['my first value', 'second value']
Object.keys(MyEnum) // returns ["FIRST", "SECOND"]
if ((<any>Object).values(Vehicle).includes('car')) {
// Do stuff here
}
Code Example |
---|
:: |
Typescript :: |
:: |
Typescript :: mui color typography |
Typescript :: |
:: circle dot in latex |
:: |
:: |
:: |
Typescript :: |
:: media breakpoints bootstrap 4 |
:: |
:: |
Typescript :: |
:: |
Typescript :: |
Typescript :: |
:: react typescript props |
:: |
:: |
:: |
:: |
:: |
Typescript :: |
:: |
:: |
:: typescript api request header |
Typescript :: |
:: typescript generic record |
:: arrow function in ts |