在控制台上,
> a = new Number(1)
Number {1}
> b = new Number(2)
Number {2}
> a + b
3
使用Type Script,
let a: Number = new Number(1);
let b: Number = new Number(2);
a + b;
给出错误:
> tsc file.ts
file.ts(3,1): error TS2365: Operator '+' cannot be applied to types 'Number' and 'Number'.
如何计算基本类型number
/ string
/ boolean
的包装器对象?
答案 0 :(得分:2)
您可以使用.valueOf()
来获取号码:
let a: Number = new Number(1);
let b: Number = new Number(2);
a.valueOf() + b.valueOf();
或使用as number
:
(a as number) + (b as number)