TS keyof typeof <Object> 不允许赋值 object.keys(<Object>)?
TS keyof typeof <Object> does not allow object.keys(<Object>) to be assigned?
我不明白这段代码有什么问题。
这是我的代码:
export type SportsTypes = keyof typeof SportsIcons
export const sports: SportsTypes[] = Object.keys(SportsIcons);
我正在将我的变量输入到一个对象的键中。
但是,当我随后尝试将一组所述键分配给该变量时,出现 ts 错误:
Type 'string[]' is not assignable to type '("Football" | "GameController" | "Tennis" | "Basketball" | "PingPong" | "Volleyball" | "HockeyPuck" | "Chess" | "AmericanFootball" | "Baseball" | "SandVolleyball")[]
export type SportsTypes = keyof typeof SportsIcons
export const sports = Object.keys(SportsIcons) as SportsTypes[];
我不明白这段代码有什么问题。
这是我的代码:
export type SportsTypes = keyof typeof SportsIcons
export const sports: SportsTypes[] = Object.keys(SportsIcons);
我正在将我的变量输入到一个对象的键中。
但是,当我随后尝试将一组所述键分配给该变量时,出现 ts 错误:
Type 'string[]' is not assignable to type '("Football" | "GameController" | "Tennis" | "Basketball" | "PingPong" | "Volleyball" | "HockeyPuck" | "Chess" | "AmericanFootball" | "Baseball" | "SandVolleyball")[]
export type SportsTypes = keyof typeof SportsIcons
export const sports = Object.keys(SportsIcons) as SportsTypes[];