Search code examples
javascriptreactjstypescripttypeofkeyof

TS keyof typeof <Object> does not allow object.keys(<Object>) to be assigned?


I do not understand what the problem is with this code.

Here is my code:

export type SportsTypes = keyof typeof SportsIcons

export const sports: SportsTypes[] = Object.keys(SportsIcons);

I am typing my variable to the keys of an object. But, when I then try to assign an array of said keys to this variable, I get the ts error: Type 'string[]' is not assignable to type '("Football" | "GameController" | "Tennis" | "Basketball" | "PingPong" | "Volleyball" | "HockeyPuck" | "Chess" | "AmericanFootball" | "Baseball" | "SandVolleyball")[]


Solution

  • export type SportsTypes = keyof typeof SportsIcons
    
    export const sports = Object.keys(SportsIcons) as SportsTypes[];