在这里,我试图检查播放器界面中的boostersToGo变量是否存在“助推器”(Card [])。如果有的话,我想转移()它并使之成为currentBooster。在与Typescript的接口中处理(type | undefined)检查
然而,打字稿是给我的错误:
message: 'Type 'Card[] | undefined' is not assignable to type 'Card[]'.
Type 'undefined' is not assignable to type 'Card[]'.'
我的编辑(VSCode)在红这里强调player.currentBooster。这是怎么回事?在使用打字稿之前,我遇到了类似的问题,但是他们已经在他们身上劈了一下。处理这个问题的正确方法是什么?
if (player.boostersToGo.length > 0) {
player.currentBooster = player.boostersToGo.shift()
}
//player is guaranteed to fit the interface Player because that's what's explicitly passed into the function
export interface Player {
boostersToGo: Card[][]
currentBooster: Card[]
picks: Card[][]
human: boolean
}
export interface Card {
name: string
manaCost: string
colors?: string[]
cmc: number
types: string[]
rarity: string
imageUrl: string
pick?: boolean
}
谢谢!
试试这个 player.boostersToGo.shift() –
Sreemat
固定。我想我必须投下所有可能未定义的东西?谢谢。 –