Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
Compruebe si existe una cadena en const typescript

tengo una const como esta

 export class CustomerType { static readonly main = 'mainCustomer'; static readonly additional = 'additionalCustomer'; }

Y yo tengo

 const value = 'main'

¿Es posible verificar si existe valor en CustomerType y devolver verdadero o falso? Sé que puedo usar include cuando es una matriz pero esta es una clase.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede usar Object.keys() para obtener todas las propiedades de la class y luego usar Array.prototype.includes() para ver si está contenido:

 class CustomerType { static readonly main = 'mainCustomer'; static readonly additional = 'additionalCustomer'; } const value = 'main' console.log(Object.keys(CustomerType).includes(value))

Puede ver esto funcionando en el patio de juegos mecanografiado .

about 4 years ago · Juan Pablo Isaza Report

0

type valueType = keyof typeof CustomerType const value: valueType = 'main'; // no error const value2: valueType = 'something else'; // throws typescript error

Vea un ejemplo en el patio de juegos mecanografiado aquí .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!