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

157
Views
Typescript "Element implicitly has an 'any' type because expression of type 'Rank' can't be used to index type 'IPowerCards'"

I am building a card game and have the following types:

type Rank = "A"| "2"| "3"| "4"| "5"| "6"| "7"| "8"| "9"| "T"| "J"| "Q"| "K";

interface IPowerCards {
  [key: Rank]: any
}

I also have the following object which I am trying to access with a rank string:

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

All the keys on this object are valid Rank types. I am trying to access values on the powerCards object using the following code:

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

However, I get the 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'. This error message doesn't make sense to me because I am ensuring that the rank variable, which is the variable I am using to access the powerCards object, is of Rank type, which is what I have specified in my IPowerCards type definition. Where am I going wrong? Thanks

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use a Record for IPowerCards instead:

type IPowerCards = Record<Rank, any>

If you want to have the properties optional:

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!