首页 > 解决方案 > 类型“未定义”不能分配给构造函数定义中的类型“数字”

问题描述

问题

参数值maxORhealth提示如下错误;在 VS 代码中:

Type 'undefined' is not assignable to type 'number'.ts(2345)

...并且不确定如何解决此错误。

类定义

    class Vitals implements IVitals
    {
        vitals!: 
        { 
            HealthValue:    number | undefined; 
            StrengthValue?: number | undefined;
            ManaValue?:     number | undefined; 
            StaminaValue?:  number | undefined; 
            IntelectValue?: number | undefined; 
            TuneValue?:     number | undefined; 
        };

        /// CONSTRUCTORS
        constructor()
        constructor(maxORhealth: number)
        constructor(maxORhealth: number, maxORstrength: number)
        constructor(maxORhealth: number, 
                    maxORstrength: number, 
                    mana: number, 
                    stamina: number, 
                    intelect: number, 
                    tune: number)
        constructor(maxORhealth?: number, 
                    maxORstrength?: number, 
                    mana?: number, 
                    stamina?: number, 
                    intelect?: number, 
                    tune?: number)
    {
           if (Number.isInteger(maxORhealth) && maxORstrength === undefined && mana === undefined)
           {
                this.vitals.HealthValue   = getRandomInt(maxORhealth);  <= Error .ts(2345)
                this.vitals.StrengthValue = getRandomInt(maxORhealth);
                this.vitals.ManaValue     = getRandomInt(maxORhealth); 
                this.vitals.StaminaValue  = getRandomInt(maxORhealth); 
                this.vitals.IntelectValue = getRandomInt(maxORhealth); 
                this.vitals.TuneValue     = getRandomInt(maxORhealth); 
           }
           else if (Number.isInteger(maxORhealth) && maxORstrength === undefined)
           {
                this.vitals.HealthValue   = maxORhealth; 
                this.vitals.StrengthValue = getRandomInt(maxORstrength!);
                this.vitals.ManaValue     = getRandomInt(maxORstrength!); 
                this.vitals.StaminaValue  = getRandomInt(maxORstrength!); 
                this.vitals.IntelectValue = getRandomInt(maxORstrength!); 
                this.vitals.TuneValue     = getRandomInt(maxORstrength!); 
            }
            else
            {
                this.vitals.HealthValue   = maxORhealth; 
                this.vitals.StrengthValue = maxORstrength;
                this.vitals.ManaValue     = mana; 
                this.vitals.StaminaValue  = stamina; 
                this.vitals.IntelectValue = intelect; 
                this.vitals.TuneValue     = tune; 
            }
        }
    }

期望的影响

我想constructors为这门课准备三个,如下所示:

    constructor()
    constructor(maxORhealth: number)
    constructor(maxORhealth: number, maxORstrength: number)
    constructor(maxORhealth: number, maxORstrength: number, mana: number, stamina: number, intelect: number, tune: number)

...(希望)有一个实用的(不过分 JS 喜欢)解决方案。

标签: typescripttypesconstructorconstructor-overloading

解决方案


错误源于此语句:

Number.isInteger(maxORhealth)

maxORhealth是类型number | undefined。因为它是构造函数的可选参数,所以它可以是 anumberundefined.

Number.isInteger被键入以仅接受 type 的参数number。因此 TypeScript 抱怨因为maxORhealth也可能是一个数字。


要解决此问题,您可以执行以下两项操作之一。您可以像这样引入额外的检查undefined

if (maxORhealth !== undefined && Number.isInteger(maxORhealth) && maxORstrength === undefined && mana === undefined) {
//  ^^^^^^^^^^^^^^^^^^^^^^^^^

操场

或者,考虑到Number.isInteger(undefined)处理得当并且只是返回false,你也可以说服 TypeScript 无论如何它都可以。为此,您可以使用非空断言后缀!运算符:

Number.isInteger(maxORhealth!)
//                          ^

这个操作符告诉 TypeScript that maxORhealthis notundefined和 not null

操场


推荐阅读