简体   繁体   English

TypeScript:'Card | 类型的参数 undefined' 不可分配给类型为 'Card' 的参数

[英]TypeScript: Argument of type 'Card | undefined' is not assignable to parameter of type 'Card'

I am trying to make Blackjack as an exercise to learn TypeScript.我正在尝试将二十一点作为学习 TypeScript 的练习。 When I run the program, the player gets two cards from the shoe succesfully, but I get the warning Argument of type 'Card | undefined' is not assignable to parameter of type 'Card'.当我运行程序时,玩家成功地从鞋子里拿到了两张牌,但是我得到了Argument of type 'Card | undefined' is not assignable to parameter of type 'Card'.的警告Argument of type 'Card | undefined' is not assignable to parameter of type 'Card'. Argument of type 'Card | undefined' is not assignable to parameter of type 'Card'.

Here's my code:这是我的代码:

class Card {
    suit: string;
    value: number;
    name: string;
    constructor(suit: string, value: number, name: string) {
        this.suit = suit;
        this.value = value;
        this.name = name;
    }
    greet() {
        console.log(`This card is a ${this.value} of ${this.suit}`)
    }
}

class Deck {
    static suits = ["Diamonds", "Clubs", "Hearts", "Spades"]
    static cardTypes = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
    cards: Card[] = [];
    makeDeck() {
        let cards: Card[] = [];
        for (let i = 0; i < Deck.suits.length; i++) {
            for (let j = 0; j < Deck.cardTypes.length; j++) {
                let value = Deck.cardTypes[j];
                let numValue: number;
                let name: string;
                if (
                    value === "Jack" ||
                    value === "Queen" ||
                    value === "King"
                ) {
                    numValue = 10
                } else if (value === "Ace") {
                    numValue = 11
                }
                else {
                    numValue = parseInt(Deck.cardTypes[j]);
                }

                name = `${value} of ${Deck.suits[i]}`

                cards.push(new Card(Deck.suits[i], numValue, name))
            }
        }
        return cards;
    }
    constructor() {
        this.cards = this.makeDeck()
    }
    presentDeck() {
        console.log(this.cards)
    }
}

class Shoe {
    numberOfDecks: number;
    cards: Card[] = [];
    constructor(numberOfDecks: number) {
        this.numberOfDecks = numberOfDecks
        this.cards = this.makeShoe()
    }
    makeShoe() {
        let cards: Card[] = [];
        for (let i = 0; i < this.numberOfDecks; i++) {
            let deck = new Deck();
            for (let j = 0; j < deck.cards.length; j++) {
                cards.push(deck.cards[j])
            }
        }
        return cards;
    }
    shuffleCards(cards: Card[] = this.cards) {
        var j, x, i;
        for (i = cards.length - 1; i > 0; i--) {
            j = Math.floor(Math.random() * (i + 1));
            x = cards[i];
            cards[i] = cards[j];
            cards[j] = x;
        }
        return cards;
    }
    presentShoe() {
        console.log(this.cards)
    }
    dealCard() {
        return this.cards.pop();
    }
}

class Hand {
    private _cards: Card[] = [];
    receiveCard(card: Card) {
        this.cards.push(card);
    }
    get cards(): Card[] {
        return this._cards
    }
}

let deck = new Deck();
let shoe = new Shoe(2);
let player = new Hand();

shoe.shuffleCards();
player.receiveCard(shoe.dealCard());
player.receiveCard(shoe.dealCard());
console.log(player.cards)

The error is on the shoe.dealCard() parts at the end.错误出现在最后的shoe.dealCard()部分。 I'm not sure why the type has two parts, Card and undefined.我不确定为什么该类型有两个部分,Card 和 undefined。 And I'm not sure what is undefined.而且我不确定什么是未定义的。

The error happens because this.cards.pop() might return undefined when this.cards has no elements ( this.cards === [] ).发生错误是因为当this.cards没有元素( this.cards === [] )时, this.cards.pop()可能返回undefined One way to make the error disappear is to use the non-null assertion operator !使错误消失的一种方法是使用非空断言运算符! . . This operator accepts that this expression will never be null and doesn't show the error anymore.此运算符接受此表达式永远不会为空并且不再显示错误。

dealCard() {
    return this.cards.pop()!;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 typescript '严格' - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. typescript 'strict' Typescript:类型参数'string | undefined' 不能分配给“string”类型的参数 - Typescript: Argument of type 'string | undefined' is not assignable to parameter of type 'string' 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error Typescript:a 类型的参数不能分配给 b 类型的参数 - Typescript: argument of type a is not assignable to parameter of type b Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' TypeScript:类型参数不可分配 - TypeScript: Argument of type is not assignable 'string | 类型的参数号码 | 字符串[] | 将 JavaScript 转换为 TypeScript 时,undefined' 不可分配给“字符串”类型的参数 - Argument of type 'string | number | string[] | undefined' is not assignable to parameter of type 'string' when converting JavaScript to TypeScript typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345) Typescript - 'string | 类型的参数 (string | null)[]' 不能分配给“string”类型的参数 - Typescript - Argument of type 'string | (string | null)[]' is not assignable to parameter of type 'string' React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM