javascript - Handling (type | undefined) checks in interfaces with Typescript -
here, i'm trying check if there's "booster" (card[]) in boosterstogo variable in player interface. if there is, want shift() , make currentbooster.
however, typescript giving me error:
message: 'type 'card[] | undefined' not assignable type 'card[]'. type 'undefined' not assignable type 'card[]'.'
my editor (vscode) underlining player.currentbooster in red here. what's going on? have had similar issues before using typescript have hacked around them little bit. what's proper way deal this?
if (player.boosterstogo.length > 0) { player.currentbooster = player.boosterstogo.shift() } //player guaranteed fit interface player because that's what's explicitly passed 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 }
thanks!
i not reproduce in http://www.typescriptlang.org, maybe it's related typescript
/ intellisense
version, change lib.d.ts
definitions.
it may worth trying run bigger portion of problematic code in there , see if problem persists. if doesn't, maybe upgrading latest typescript
version or updating vscode
help.
otherwise, can enforce type type assertion (casting):
if (player.boosterstogo.length > 0) { player.currentbooster = player.boosterstogo.shift() card[]; }
Comments
Post a Comment