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

236
Views
Comprobación de mecanografiado si un valor es una cadena

tengo un componente:

 export type IDAta = string | number | null; interface IDAta { data?: string | number | null; verify: boolean; } const App: React.FC<IDAta> = ({ data = '', verify = true, }) => { if ((data || data === 0) { return ( <div> {verify && isText(data) ? ( <Child text={data} /> ) : ( data )} </div> ); } }; export default App;

isText y el componente Child esperan que los parámetros solo sean una cadena, pero al hacer esto, verify && isText(data) , luego obtengo un error en mecanografiado como Type 'number' is not assignable to type 'string'. . Esto podría resolverse de la siguiente manera:

 { verify && typeof data === 'string' && isText(data) ? ( <Child text = {data as string}/> ) : ( data ) }

No estoy seguro de si esta solución está bien, porque typeof data === 'string' duplica la lógica de la función isText que se ve así:

 const isText = (data: string) => { return typeof data === 'string'; };

Pregunta: ¿Cuál será la mejor solución para resolver el problema descrito anteriormente?

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

0

Una función de guardia/predicado de tipo :

 function isText(data: any): data is string { return typeof data === 'string'; };
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!