Home > OS >  How to specify a type for list items. TypeScript error (TS7053)
How to specify a type for list items. TypeScript error (TS7053)

Time:09-27

There is this code, with a TS error(TS7053) here- ${Titles[item]} I know it's a simple error. I'm new to TS, I don't understand how I need to specify a type for ${Titles[details]} to make this code work

import { Details } from './details';
import { Titles } from './titles';

type Keys = keyof typeof Titles;

interface Props {
  details: Details;
} 

export function TailsList({ details }: Props) {
  const keysList: string[] = Object.keys(Titles) as Keys[];
  return (
    <ul>
      {keysList?.map((item) => (
        <li key={item}>{`${Titles[item]}`}</li>
      ))}
    </ul>
)

CodePudding user response:

Remove the string[] from keysList.

keyList was typed as string[] instead of Keys[].

CodePudding user response:

Put it as:

 const keysList: Keys[] = Object.keys(Titles) as Keys[];
  • Related