wynx
wynx

Reputation: 313

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")[]

Upvotes: 0

Views: 553

Answers (1)

TheWuif
TheWuif

Reputation: 1036

export type SportsTypes = keyof typeof SportsIcons

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

Upvotes: 1

Related Questions