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

147
Views
Escriba el tipo extendido si el tipo booleano es verdadero

Me gustaría saber si es posible extender un tipo dependiendo del valor de otro tipo.

 interface NavType { hideNav?: boolean dev?: boolean }

Me gustaría permitir el uso de hideNav solo si el dev arg es falso.

 const Navbar: React.FC<NavType> = ({ children, hideNav, dev = false }) => ( <nav> {!dev ? ( <NavDefault hideNav={!!hideNav}>{children}</NavDefault> ) : ( <NavDev>{children}</NavDev> )} </nav> )

¿Cómo puedo usar condicionales en este caso?

 // Should PASS const navArgs1: NavType = { hideNav: undefined, dev: true } const navArgs2: NavType = { hideNav: true or false, dev: false } // Should NOT PASS const navArgs3: NavType = { hideNav: true or false, dev: true }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede definir 2 tipos y dejar que las propiedades del componente sean su unión; en principio lo siguiente (mostrado como código TS Playground ):

 interface NavBase { dev: boolean; } interface DevNavType extends NavBase { dev: true; hideNav?: undefined; } interface NotDevNavType extends NavBase { dev: false; hideNav: boolean; } // This simulates your component function f(x: DevNavType | NotDevNavType) { } f({ dev: true }); // Works f({ dev: true, hideNav: undefined }); // Also works f({ dev: false, hideNav: true }); // Works too - for any boolean value of hideNav f({ dev: false }); // Compiler error f({ dev: true, hideNav: true }); // Compiler error

Notas:

  1. Realmente no necesitas la interfaz base, NavBase
  2. Es posible que desee definir un tipo para la unión, para mantener el código claro: type NavType = DevNavType | NotDevNavType
over 4 years ago · Santiago Trujillo 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!