类型 'undefined' 不可分配给构造函数定义中的类型 'number'

Type 'undefined' is not assignable to type 'number' within constructor definition

Issue

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

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

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

Class Definition

    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; 
            }
        }
    }

Desired Affect

这个class我要三个constructors,如下:

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

...(希望)有一个实用的(none 过于花哨的 JS)解决方案。

错误源于此语句:

Number.isInteger(maxORhealth)

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

Number.isInteger 的类型只接受 number 类型的参数。因此 TypeScript 会抱怨,因为 maxORhealth 也可能是一个数字。


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

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

Playground

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

Number.isInteger(maxORhealth!)
//                          ^

这个运算符告诉 TypeScript maxORhealth 不是 undefined 也不是 null.

Playground