typescript - 类型“未定义”不能分配给构造函数定义中的类型“数字”
问题描述
问题
参数值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 喜欢)解决方案。
- 干杯,并提前感谢。
解决方案
错误源于此语句:
Number.isInteger(maxORhealth)
maxORhealth
是类型number | undefined
。因为它是构造函数的可选参数,所以它可以是 anumber
或undefined
.
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 maxORhealth
is notundefined
和 not null
。
推荐阅读
- python - 获取每组熊猫的前 n 行
- ruby-on-rails - 在elasticsearch中设置关键字字段
- powerbi - 如何将资源合二为一?
- qt - qt.qpa 插件:在“”中找不到 qt 平台插件“windows”
- android - EditText“缺少 autofillHints 属性”和“缺少辅助功能标签”
- php - 将文件从 Gitlab 的 master 分支复制到我的服务器
- nusmv - 属性检查的执行时间
- javascript - 如何在给定时间内重新加载ajax调用函数
- c# - 将几何图形设置为全屏
- java - jasper 报告多语言 - 在 spring boot 中生成 pdf 时无法显示字体