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

151
Views
Texto mecanografiado "El elemento tiene implícitamente un tipo 'cualquiera' porque la expresión de tipo 'Rank' no se puede usar para indexar el tipo 'IPowerCards'"

Estoy construyendo un juego de cartas y tengo los siguientes tipos:

 type Rank = "A"| "2"| "3"| "4"| "5"| "6"| "7"| "8"| "9"| "T"| "J"| "Q"| "K"; interface IPowerCards { [key: Rank]: any }

También tengo el siguiente objeto al que intento acceder con una cadena de clasificación:

 const powerCards: IPowerCards = { "A": { canPlayOnAnyCard: true, canChangeSuit: true, }, "2": { canCounterPenaltyCard: true, penaltyAmount: 2, }, ..., };

Todas las claves de este objeto son tipos de Rank válidos. Estoy tratando de acceder a los valores en el objeto powerCards usando el siguiente código:

 const rank = getRank(card) as Rank; return rank && powerCards[rank]?.canCounterPenaltyCard

Sin embargo, aparece el error Element implicitly has an 'any' type because expression of type 'Rank' can't be used to index type 'IPowerCards'. Property 'A' does not exist on type 'IPowerCards'. Este mensaje de error no tiene sentido para mí porque me aseguro de que la variable de rank , que es la variable que estoy usando para acceder al objeto powerCards , sea del tipo Rank , que es lo que especifiqué en mi definición de tipo IPowerCards . ¿Dónde me estoy equivocando? Gracias

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use un Record para IPowerCards en su lugar:

 type IPowerCards = Record<Rank, any>

Si desea tener las propiedades opcionales:

 type IPowerCards = { [key in Rank]?: any }
about 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!