我不明白這段代碼有什么問題。
這是我的代碼:
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")[]
uj5u.com熱心網友回復:
export type SportsTypes = keyof typeof SportsIcons
export const sports = Object.keys(SportsIcons) as SportsTypes[];
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/372378.html
標籤:javascript 反应 打字稿 类型 关键
