我有一些像这样的常量
export class CustomerType {
static readonly main = 'mainCustomer';
static readonly additional = 'additionalCustomer';
}
我有
const value = 'main'
是否可以检查 CustomerType 中是否存在值并返回 true 或 false? 我知道我可以使用 include 什么时候是数组但这是类?
最佳答案
您可以使用 Object.keys()
获取 class
的所有属性,然后使用 Array.prototype.includes()
查看它是否包含:
class CustomerType {
static readonly main = 'mainCustomer';
static readonly additional = 'additionalCustomer';
}
const value = 'main'
console.log(Object.keys(CustomerType).includes(value))
您可以在 the typescript playground 中看到这个工作.
关于javascript - 检查常量 typescript 中是否存在字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70973762/