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

350
Views
Un tipo de parámetro de firma de índice no puede ser un tipo de unión. Considere usar un tipo de objeto mapeado en su lugar

Estoy tratando de usar el siguiente patrón:

 enum Option { ONE = 'one', TWO = 'two', THREE = 'three' } interface OptionRequirement { someBool: boolean; someString: string; } interface OptionRequirements { [key: Option]: OptionRequirement; }

Esto me parece muy sencillo, sin embargo, aparece el siguiente error:

Un tipo de parámetro de firma de índice no puede ser un tipo de unión. Considere usar un tipo de objeto asignado en su lugar.

¿Qué estoy haciendo mal?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede usar el operador TS "en" y hacer esto:

 enum Options { ONE = 'one', TWO = 'two', THREE = 'three', } interface OptionRequirement { someBool: boolean; someString: string; } type OptionRequirements = { [key in Options]: OptionRequirement; // Note that "key in". }
over 4 years ago · Santiago Trujillo Report

0

La solución más simple es usar Record

 type OptionRequirements = Record<Options, OptionRequirement>

También puede implementarlo usted mismo como:

 type OptionRequirements = { [key in Options]: OptionRequirement; }

Esta construcción sólo está disponible para type , pero no como interface .

El problema en su definición es decir que la clave de su interfaz debe ser del tipo Options , donde Options es una enumeración, no una cadena, un número o un símbolo.

La key in Options significa "para esas claves específicas que están en el tipo de unión Opciones".

type alias es más flexible y potente que interface .

Si su tipo no necesita ser usado en clase, elija type sobre interface .

over 4 years ago · Santiago Trujillo Report

0

En mi caso:

 export type PossibleKeysType = | 'userAgreement' | 'privacy' | 'people'; interface ProviderProps { children: React.ReactNode; items: { // ↙ this colon was issue [key: PossibleKeysType]: Array<SectionItemsType>; }; }

Lo arreglé usando el operador in lugar de usar :

 ~~~ interface ProviderProps { children: React.ReactNode; items: { // ↙ use "in" operator [key in PossibleKeysType]: Array<SectionItemsType>; }; }
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!