简体   繁体   English

在 Typescript 对象中添加新属性

[英]add new property in Typescript object

I'm trying to add new property in object, but typescript given error.我正在尝试在对象中添加新属性,但打字稿给出了错误。

error TS2339: Property 'qty' does not exist on type 'Object'.错误 TS2339:“对象”类型上不存在属性“数量”。

product: Object = {qty: Number};

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

Object is the wrong annotation. Object是错误的注释。 Change your annotation:更改注释:

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

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

Number is a data type not a value. Number是一种数据类型,而不是值。 the object only accept the value not datatype.对象只接受值而不是数据类型。 So you can't declare number as a value所以你不能将 number 声明为一个值

try this simple way试试这个简单的方法

product: any;

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

Seems that you've tried to do this:似乎您已经尝试过这样做:

product: {qty: number} = {};

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

This won't work, although, because the object you've started with is incomplete.但是,这不起作用,因为您开始使用的对象不完整。 You should either mark the property as optional:您应该将该属性标记为可选:

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

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

Or provide some default value:或者提供一些默认值:

product: {qty: number | null} = {qty: null};
// or just {qty: number}, if you don't use strict null checks

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

If default value is a number too, you can even simplify this by using type inference:如果默认值也是一个数字,你甚至可以通过使用类型推断来简化它:

product = {qty: 0};

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

All these three examples will work (if you provide proper value for this in function foo , of course, but I assume that you'll do it anyway).所有这三个示例都可以工作(当然,如果您在函数foo this提供了适当的值,但我假设您无论如何都会这样做)。

Change Object to any type将对象更改为任何类型

product: any = {qty: Number};

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

Use this用这个

product = {qty: null};

OR或者

product = {qty: 0};

instead of this -而不是这个 -

product: Object = {qty: Number};

Because you initializing your object with key and value as type which is number so instead of assigning type you need to set some value which is anything say 0 or null or anything you want as the default value.因为你初始化密钥和值作为对象typenumber那么不是分配型的你需要设置一些值是什么,比如0null或任何你想要作为默认值。

You can also do like this and add as many as new properties you want in a product您也可以这样做并在产品中添加任意数量的新属性

product: Object = new Object();

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

A solution without extra code would be a config option to allow the creation of new properties with the assignment operator but since there is no such property i patched the typescript code to allow it.一个没有额外代码的解决方案是一个配置选项,允许使用赋值运算符创建新属性,但由于没有这样的属性,我修补了打字稿代码以允许它。

Do this on both tsc.js and typescript.js, those files are in node_modules/typescript在 tsc.js 和 typescript.js 上都这样做,这些文件在 node_modules/typescript

Find this line找到这一行

 if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) 

And paste this right under it并将其粘贴在其正下方

return getTypeOfSymbol(right);

I have to admit that im not a typescript src code developer and i havent tested extensively the side effects of doing this but it works for my projects我不得不承认,我不是打字稿 src 代码开发人员,我还没有广泛测试这样做的副作用,但它适用于我的项目

The linter will show an error but it will compile fine linter 将显示一个错误,但它会编译正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM