在Typescript对象中添加新属性 [英] add new property in Typescript object

查看:90
本文介绍了在Typescript对象中添加新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在对象中添加新属性,但打字稿给出了错误.

I'm trying to add new property in object, but typescript given error.

错误TS2339:类型对象"上不存在属性数量".

error TS2339: Property 'qty' does not exist on type 'Object'.

product: Object = {qty: Number};

foo(){
  this.product.qty = 1;
}

推荐答案

Object 是错误的注释.更改注释:

Object is the wrong annotation. Change your annotation:

product: {qty: number} = {qty: 0};

foo(){
  this.product.qty = 1;
}

这篇关于在Typescript对象中添加新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆